我是一名初学计算机科学的学生,正在经历 Java 挑战。我们不得不编写一个有错误的程序;这实际上是一个有趣的练习。我的一个同学给我发了一个补丁(英文不是 diff)并说他添加public
到课堂上。
这让我很纳闷。重要还是只是声明类的约定public
。该程序有效,据我了解,默认值是包私有的,我认为这最适合 CS 学生互相邮寄的蹩脚的小脚本。
谁能给我一些更深入的了解——主要是关于 Java,但一般的 CS 理论也可能证明是有见地的——也许我可以用一些术语来进一步研究这些概念。
我想知道我同学的改正是否有效和或重要以及为什么。
编辑
对不起这是我的原始程序没有错误
import java.util.Scanner;
/**
* Universal hello translator
* Author: Noel Niles mail: noelniles@gmail.com
*
*/
class HelloUniverse {
public static void main (String [] args)
{
/* code */
int country; //number of country from list
String name; //name of person
String greeting; //hello in different languages
Scanner key = new Scanner(System.in);
System.out.print("Where are you from?\n"+
"Enter a number from the list:\n");
//TODO(anyone): Add some more countries and greetings
System.out.print("1. Afganistan\n" +
"2. Antarctica\n" +
"3. Australia\n" +
"4. Austria\n" +
"5. Bangladesh\n" +
"6. Belgium\n" +
"7. Brazil\n" +
"8. Burma\n" +
"9. Canada\n" +
"10. Chile\n" +
"11. China\n" );
//get the country code
country = key.nextInt();
key.nextLine();
//get the users name
System.out.println("What is you name?");
name = key.nextLine();
switch (country) {
case 1: greeting = "salaam";
break;
case 2: greeting = "h-h-h-i-i thththththere";
break;
case 3: greeting = "G'day mate";
break;
case 4: greeting = "Gruss Gott";
break;
case 5: greeting = "nomoskar";
break;
case 6: greeting = "Hallo";
break;
case 7: greeting = "Ola";
break;
case 8: greeting = "mingalaba";
break;
case 9: greeting = "Good day eh";
break;
case 10: greeting = "Hola";
break;
case 11: greeting = "Nei hou";
break;
default: greeting = "Invalid country";
break;
}
//display the greeting and the users name
System.out.printf("%s %s\n", greeting, name);
}
}