Chuyển code C sang code Java

Mình code bài tập này từ C giờ mình muốn chuyển nó sang java thi làm như thế nào vậy mấy bạn?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
/* HIDE FUNCTION FOR DEVELOPER: 205. Print current i */
char del1[8],del2[50];
struct student{
    char sid[8];
    char sname[50];
    char sdob[50];
    char semail[50];
    char sphone[50];
    char saddress[50];
} student_list [1000];
void add(int *i){
    g12:
    printf("Input student ID: "); fflush(stdin); gets(student_list[*i].sid);
    if (strlen(student_list[*i].sid)==7)
{
	int result1, result2, result3;
	result1 = strncmp(student_list[*i].sid,"GC", 2);
	result2 = strncmp(student_list[*i].sid,"GT", 2);
	int j=2;
	do {
   		 result3=isdigit(student_list[*i].sid[j]);
    		 j++;
   		 if (result3==0)
     		   break;
	   }
while (j< strlen(student_list[*i].sid)-1);
if ((result1 ==0) || (result2 ==0))
    {if (result3 !=0)
	goto g11;
    else
        {printf("The Student ID is not valid.(GTxxxxx or GCxxxxx)Press *Enter* to input again! "); getchar();
	goto g12;}}
    else
      	{printf("The Student ID is not valid.(GTxxxxx or GCxxxxx)Press *Enter* to input again! "); getchar();
	goto g12;}
}
	 else
      	{printf("The Student ID is not valid.(GTxxxxx or GCxxxxx)Press *Enter* to input again! (GTxxxxx or GCxxxxx)"); getchar();
	goto g12;}
g11:
    printf("Input student name: "); fflush(stdin); gets(student_list[*i].sname);
    printf("Input student date of birth: ");fflush(stdin); gets(student_list[*i].sdob);
    printf("Input student email: ");fflush(stdin); gets(student_list[*i].semail);
    printf("Input student sphone: ");fflush(stdin); gets(student_list[*i].sphone);
    printf("Input student address: ");fflush(stdin); gets(student_list[*i].saddress);
    *i=*i+1;
}
search (int i){
char *test;
char s[50],x1[50],x2[50];
int u=0;
printf("Please enter student's name or a part of it: "); fflush(stdin);gets(s);
int a;
for (a=0;a<i;a++)
{
    strcpy(x1,student_list[a].sname);
    strcpy(x2,s);
    test = strstr(strlwr(x1),strlwr(x2));
    if (test){
        printf("%s | %s | %s | %s | %s | %s |\n",student_list[a].sid,student_list[a].sname,student_list[a].sdob,student_list[a].semail,student_list[a].sphone,student_list[a].saddress);
        u++;}
}
if (u==0) printf("No data found!");
getchar();
}
void update(int i){
int test;
char ud[8],x1[8],x2[8];
int u=0;
int c,up;
printf("Enter student ID: ");fflush(stdin); gets(ud);
int a;
for (a=0;a<i;a++)
{
    strcpy(x1,student_list[a].sid);
    strcpy(x2,ud);
    test = strcmp(strlwr(x1),strlwr(x2));
//a chinh la gia tri THUC cua position trong maang
if (test==0)
{
printf("Current student ID: %s\n",student_list[a].sid);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new ID: "); fflush(stdin);gets(student_list[a].sid);}
printf(" Current student name: %s\n",student_list[a].sname);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new name: "); fflush(stdin);gets(student_list[a].sname);}
printf(" Current student date of birth: %s\n",student_list[a].sdob);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new date of birth: "); fflush(stdin);gets(student_list[a].sdob);}
printf(" Current student email: %s\n",student_list[a].semail);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new email: "); fflush(stdin);gets(student_list[a].semail);}
printf(" Current student phone: %s\n",student_list[a].sphone);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new phone: "); fflush(stdin);gets(student_list[a].sphone);}
printf(" Current student address: %s\n",student_list[a].saddress);
printf("Enter 1 to update & 0 to keep: "); scanf("%d",&up);
if (up==1)
	 {printf("Enter a new address: ");fflush(stdin); gets(student_list[a].saddress);}
printf("Successfully Update");
getchar();
}
}
}
void view(int i){
    int a;
    for (a=0;a<5;a++)
    printf("%s | %s | %s | %s | %s | %s |\n",student_list[a].sid,student_list[a].sname,student_list[a].sdob,student_list[a].semail,student_list[a].sphone,student_list[a].saddress);
    if (i==0) printf("No data to show!\n");
    getchar();
}
void del(int *i){
int test;
char del[8],x1[8],x2[8];
int u=0;
int c;
printf("Enter student ID: ");fflush(stdin); gets(del);
int a;
for (a=0;a<(*i);a++)
{
    strcpy(x1,student_list[a].sid);
    strcpy(x2,del);
    test = strcmp(strlwr(x1),strlwr(x2));
//a chinh la gia tri cua position trong mang
                if (test==0){
                                for ( c = a ; c < *i ; c++ )
                                    {strcpy(student_list[c].sid,student_list[c+1].sid);
                                     strcpy(student_list[c].sname,student_list[c+1].sname);
                                     strcpy(student_list[c].sdob,student_list[c+1].sdob);
                                     strcpy(student_list[c].semail,student_list[c+1].semail);
                                     strcpy(student_list[c].sphone,student_list[c+1].sdob);
                                     strcpy(student_list[c].saddress,student_list[c+1].saddress);}
                                 u++;
                                    {strcpy(student_list[*i].sid,del1);
                                     strcpy(student_list[*i].sname,del2);
                                     strcpy(student_list[*i].sdob,del2);
                                     strcpy(student_list[*i].semail,del2);
                                     strcpy(student_list[*i].sphone,del2);
                                     strcpy(student_list[*i].saddress,del2);}
                         }
}
if (u==0) printf("Student ID doesn't exist!");
getchar();
*i=*i-1;
}
void menu(){
    printf("\t\t************************\n");
    printf("\t\t* 1. Add new student   *\n");
    printf("\t\t* 2. View all students *\n");
    printf("\t\t* 3. Search students   *\n");
    printf("\t\t* 4. Delete students   *\n");
    printf("\t\t* 5. Update student    *\n");
    printf("\t\t* 6. Exit              *\n");
    printf("\t\t************************\n");
    printf("\n");
}
int main(){
    int choice,i=0;
    g0:
    system("cls");
    menu();
//Kiem tra so nhap vao phai tu 1 den 6
    do {
    printf("Choose an option from 1 to 6: ");scanf("%d",&choice);}
    while (choice<1 || choice>6 && choice != 205);
    fflush(stdin);
    switch (choice){
        case 1: add(&i);
                break;
        case 2: view(i);
                break;
        case 3: search(i);
                break;
        case 4: del(&i);
                break;
        case 5: update(i);
                break;
        case 6: exit(0);
                break;
        case 205: 
        	printf("current i: %d",i); 
        	fflush(stdin); 
        	getchar();
        	break;
        default: ;
       
    }
    goto g0;
    getchar();
    return 0;
}

thật sự là code java?

bạn kia đang nhờ chuyển từ C qua Java mà a… code dài ghia… nhìn ngốt quá :smile:

1 Like

Thì học Java chứ làm gì nữa ?

1 Like

Mới học Java thì học từ từ đi, vì mỗi khái niệm của ngôn ngữ này có thể giống ngôn ngữ kia, và trong suốt quá trình học bạn sẽ dần nắm bắt được. Và khi đó việc chuyển bài toán từ ngôn ngữ này qua ngôn ngữ kia sẽ không mấy khó khăn :grin:

Chỉ mất công gõ thôi, java sang c++ mới đau đầu chứ c++ sang java thì có gì đâu

bài tập danh sách sinh viên , sang java dùng class thì làm vừa gọn vừa nhanh hơn c nhiều …

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?