输入和循环
我正在尝试按照图片所示进行操作:
这是我的代码:
import java.util.Scanner;
public class IcsProject
{
public static void main(String [] args)
{
Scanner keyboard= new Scanner (System.in);
int menuNum,ID,semNum,semCode,semCourses;
do{
System.out.println("Please Enter your Choice from the menu:");
System.out.println("1. Enter Student Sanscript");
System.out.println("2. Display Transcript Summary");
System.out.println("3. Read Student Franscript from a File");
System.out.println("4. Write Transcript Summary to a File");
System.out.println("5. Exit");
menuNum = keyboard.nextInt();
if (menuNum == 2 || menuNum == 3 || menuNum == 4)
System.out.println("Not working");
} while (menuNum > 1 && menuNum < 5);
//// Option 1: Enter student transcript
if (menuNum == 1)
System.out.println("Please enter your student's FIRST and LAST name:");
String stuName = keyboard.nextLine();
System.out.println("Please enter the ID number for " + stuName);
ID = keyboard.nextInt();
System.out.println("Please enter the number of semesters");
semNum = keyboard.nextInt();
for(int i=1 ; i < semNum ; i++)
{System.out.println("Please enter semester code for semester n# " + semNum);
semCode = keyboard.nextInt();
System.out.println("Please enter the number of courses taken in " + semCode );
semCourses = keyboard.nextInt();}
System.out.println("Enter course code, credit hours, and letter grade ")
///I stopped here
}
- 我必须从学期代码开始使用数组吗?请给我举个例子。
- 输入所有值后,程序应再次显示菜单,以便我可以从中进行选择。怎么做呢?
- 我在第一个问题“输入学生的名字和姓氏”时遇到问题 程序会跳过它并转到下一个问题。我的
keyboard.nextLine();
有错误吗
I'm trying to do as the picture shows here:
This is my code:
import java.util.Scanner;
public class IcsProject
{
public static void main(String [] args)
{
Scanner keyboard= new Scanner (System.in);
int menuNum,ID,semNum,semCode,semCourses;
do{
System.out.println("Please Enter your Choice from the menu:");
System.out.println("1. Enter Student Sanscript");
System.out.println("2. Display Transcript Summary");
System.out.println("3. Read Student Franscript from a File");
System.out.println("4. Write Transcript Summary to a File");
System.out.println("5. Exit");
menuNum = keyboard.nextInt();
if (menuNum == 2 || menuNum == 3 || menuNum == 4)
System.out.println("Not working");
} while (menuNum > 1 && menuNum < 5);
//// Option 1: Enter student transcript
if (menuNum == 1)
System.out.println("Please enter your student's FIRST and LAST name:");
String stuName = keyboard.nextLine();
System.out.println("Please enter the ID number for " + stuName);
ID = keyboard.nextInt();
System.out.println("Please enter the number of semesters");
semNum = keyboard.nextInt();
for(int i=1 ; i < semNum ; i++)
{System.out.println("Please enter semester code for semester n# " + semNum);
semCode = keyboard.nextInt();
System.out.println("Please enter the number of courses taken in " + semCode );
semCourses = keyboard.nextInt();}
System.out.println("Enter course code, credit hours, and letter grade ")
///I stopped here
}
- Do I have to use array starting from the semester code? show me an example please.
- After entering all the values The program should show the Menu again so I can choose from it. How to do that?
- I'm having a problem at the first question "entering the student first and last name"
The program just skip it and move to next question. Is there a mistake with mykeyboard.nextLine();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用一个对象列表,其中包含您要记录的所有字段。
例如,只需使用谷歌即可。
http://www.google.com/search?q=java+list+examples 2790 万个结果
http://www.google.com/search? q=java+object+examples 1800 万个结果。
http://www.google.com/search?q=java+array+examples 1500 万个结果。
I would use a list of objects which have all the fields you want to record.
For examples, just use google.
http://www.google.com/search?q=java+list+examples 27.9 million result
http://www.google.com/search?q=java+object+examples 18 million results.
http://www.google.com/search?q=java+array+examples 15 million results.
关于问题#2 - 将菜单放在单独的方法中。使用一个循环,它的条件是菜单或类似的东西,根据菜单的结果进行处理(这是抽象的,我想你可以从这里弄清楚):
关于问题#3。您读取了一个 int:
menuNum = Keyboard.nextInt();
但该行尚未结束,因此下一个nextLine
(String StuName = Keyboard.nextLine() ;
) 占据该行的其余部分。使用nextLine()
并解析整数。Regarding issue #2 - put the menu in a separate method. use a loop that it's condition is the menu or something similar to process according to the result from menu (this is abstract, I think you can figure it out from here):
Regarding issue #3. You read an int:
menuNum = keyboard.nextInt();
but the line is not over, so the nextnextLine
(String stuName = keyboard.nextLine();
) takes the rest of the line. usenextLine()
and parse the integers instead.