在Java中进行直接用户输入

huangapple go评论130阅读模式
英文:

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(&quot;NUM\t FIRSTNAME \t LASTNAME \t MARKS \t GRADE&quot;);
	
	  int i=1;
	  String f = sc.nextLine();
	  while(i&lt;=2) {
		System.out.print(i+ &quot;\t&quot;);
		System.out.print(&quot;\t&quot; + 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 两次,用户能够输入两条不同的记录。

输出将会像这样:

在Java中进行直接用户输入

英文:

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(&quot;NUM\t FIRSTNAME\t LASTNAME\t MARKS\t GRADE&quot;);

    int i=1;

    String firstName = &quot;&quot;;
    String lastName = &quot;&quot;;
    int marks = 0;
    String grade = &quot;&quot;;

    while(i&lt;=2) {
        System.out.print(i+ &quot;\t &quot;);
        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:

在Java中进行直接用户输入

huangapple
  • 本文由 发表于 2020年5月4日 20:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61592174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定