如何显示使用实例化输入的值?

问题描述 投票:0回答:1

如何修改else if中的代码,以便在条件2中显示我在if中输入的所有内容?

import java.util.*;

public class REPORTS
{  
       public static void main(String[]args)
   {
     int Studentid,equipid,quantity,studentid,equipid1;
     String Studentname,Studentcourse,Studentlevel,equipmentname,reservationdate,returndate;

      STUDENT stud=new STUDENT(1234,"abc","abc","abc");
      EQUIPMENT equip;
      RESERVATION reserve;

      Scanner in = new Scanner(System.in);
      int x = choices();

         if(x==1)
         {             
         System.out.println("Enter Student ID:");
         Studentid=in.nextInt();
         in.nextLine();
         System.out.println("Enter Student Name:");
         Studentname=in.nextLine();
         System.out.println("Enter Student Course:");
         Studentcourse=in.nextLine();
         System.out.println("Enter Student Level:");
         Studentlevel=in.nextLine();   

         stud.setID(Studentid);
         stud.setName(Studentname);
         stud.setCourse(Studentcourse);
         stud.setLevel(Studentlevel);
         }
         else if(x==2)
         {
            stud.display();                 
         }
       }

我正在考虑使用循环,但是我不知道如何正确地循环以获取用户在if语句中输入的数据。

我将if else更改为switch,并尝试了while循环。但是该程序会无休止地运行,并且没有显示我输入的内容,而是不断询问学生姓名:

while(x!=7)
   {
     switch(x)
     {
        case 1:
     {
        stud.getData();
        choices();    
        break;
     }
        case 2:                     
     {
        stud.display(); 
        break;
     }
   }    
}
java oop instantiation composition
1个回答
1
投票

一些起点:

public static void main(String[]args)
   {
     int Studentid,equipid,quantity,studentid,equipid1;
     String Studentname, Studentcourse, Studentlevel, equipmentname, 
     reservationdate, returndate;    
      STUDENT stud=new STUDENT(1234,"abc","abc","abc");
      ...

将您的学生班级重命名为学生。另外,您不需要所有这些局部变量,它们只会使您的代码难以阅读。提供学生的默认构造函数

    public static void main(String[]args)
       {
         Student stud=new Student(); // call the default constructor, don't enter bogus data

          Scanner in = new Scanner(System.in);
          int x = choices();
          while (x != 7) {
          switch(x) {
            case 1:
             System.out.println("Enter Student ID:");
             stud.setID(in.nextInt());
             in.nextLine();
             System.out.println("Enter Student Name:");
             stud.setName(in.nextLine());
             System.out.println("Enter Student Course:");
             stud.setCourse(in.nextLine());
             System.out.println("Enter Student Level:");
             stud.setLevel(in.nextLine());
             break;
            case 2: stud.display(); break;
           }
          // this must be inside the loop!!
          x = choices();
       }
  }
© www.soinside.com 2019 - 2024. All rights reserved.