英文:
Having direct user input in java
问题
我为我之前的问题提问方式道歉,但希望我能让这个问题更清楚。我正在尝试编写一段代码,用户可以将信息放在名字(firstname)、姓氏(lastname)和成绩(marks)的列下面,但我失败了。通过我编写的这段代码,num 下面的值应该是自动的。
请帮我解决这个问题,谢谢。
package school;
import java.util.Scanner;
class Assign1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("NUM\t FIRSTNAME \t LASTNAME \t MARKS \t GRADE");
int i = 1;
String f = sc.nextLine();
while(i <= 2) {
System.out.print(i + "\t");
System.out.print("\t" + f);
i++;
}
}
}
英文:
I apologize for how I asked my previous question but hope that I can make this question clearer. I am trying to write a code where a user can place information under the columns of firstname, lastname and marks but I have failed.
With this code I have written the values under num are supposed to be automatic.
Please help me out and thank you.
package school;
import java.util.Scanner;
class Assign1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("NUM\t FIRSTNAME \t LASTNAME \t MARKS \t GRADE");
int i=1;
String f = sc.nextLine();
while(i<=2) {
System.out.print(i+ "\t");
System.out.print("\t" + f);
i++;
}
}
}
答案1
得分: 0
我想我理解了你的问题。如果我理解错了,请纠正我。
您希望将提到的列打印出来,并且用户在每一列下面输入数据。您还希望数字列自动打印数字。以下是该程序的代码:
import java.util.Scanner;
class Assign1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("NUM\t FIRSTNAME\t LASTNAME\t MARKS\t GRADE");
int i=1;
String firstName = "";
String lastName = "";
int marks = 0;
String grade = "";
while(i<=2) {
System.out.print(i+ "\t ");
firstName = sc.next();
lastName = sc.next();
marks = sc.nextInt();
grade = sc.next();
i++;
}
}
}
为了存储每种类型的列数据,我们需要不同类型的变量。接下来,在循环内部输入用户数据。由于 i=1
,循环条件 true
两次,用户能够输入两条不同的记录。
输出将会像这样:
英文:
I think I understood your question. Correct me if I'm wrong.
You want the mentioned columns to be printed and the user to input data under each column. You also want the number column to print numbers automatically. Here is the code for that program:
import java.util.Scanner;
class Assign1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("NUM\t FIRSTNAME\t LASTNAME\t MARKS\t GRADE");
int i=1;
String firstName = "";
String lastName = "";
int marks = 0;
String grade = "";
while(i<=2) {
System.out.print(i+ "\t ");
firstName = sc.next();
lastName = sc.next();
marks = sc.nextInt();
grade = sc.next();
i++;
}
}
}
To store each kind of column data, we need different variables of the proper type. Next, we are inputting data from the user while staying inside the loop. Since i=1
, the while condition is true
2 times and the user is able to enter 2 different records.
The output will be like this:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论