【发布时间】:2017-12-19 15:21:28
【问题描述】:
要求是以表格形式显示用户输入的学生详细信息。
For example:
Enter the number of students
2
Enter the student1 details
28
Science
Is the student from same country[Y/N]
N
Enter the country
Australia
Enter the Student2 details
29
Commerce
Is the Student from same country[Y/N]
Y
The student details are
Age Subject Country
28 Science Australia
29 Commerce UK
**如果学生来自同一个国家,默认值将在国家列下打印为英国。 我被困在需要在标题(年龄,姓名,国家)下以表格格式显示值以及默认值(在这种情况下为英国)的点。 我对java很陌生,无法继续前进。您的任何帮助都会对我大有裨益。 提前致谢。
我的代码是:
public class StudentTable{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
//Port obj = new Port();
int a,i;
String b = null;
System.out.println("Enter the number of students");
a = sc.nextInt();
int[] age = new int[a+1];
String[] name = new String[a+1];
for(i=1;i<a+1;i++){
System.out.println("Enter the students "+i+ " details");
age[i] = sc.nextInt();
sc.nextLine();
name[i] = sc.nextLine();
System.out.println("Is the student from same country[Y/N]");
b = sc.nextLine();
if(b=="N"){
System.out.println("Enter the country");
String country = sc.next();
return;
}
}
if(b=="Y");
String country = "India";
System.out.println("The student details are");
System.out.format("%-15s%-15s%-15s","Age","name","country");
【问题讨论】:
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用edit 链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
-
我们不是来这里先阅读你的作业,然后消化你所有的代码,看看哪里出了问题。 您准确地告诉我们您的问题是什么。
-
您好,问题出在数据需要以表格格式显示的地方
-
您好,我会确保提出的问题更加准确和切中要害
-
b == "N"和b == "Y"将失败。见stackoverflow.com/questions/513832/…。
标签: java arrays getter-setter