输入和循环

发布于 2024-11-04 17:43:10 字数 1926 浏览 3 评论 0原文

我正在尝试按照图片所示进行操作: 在此处输入图像描述

这是我的代码:

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:
enter image description 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 my keyboard.nextLine();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄兵必败 2024-11-11 17:43:10

我会使用一个对象列表,其中包含您要记录的所有字段。

例如,只需使用谷歌即可。

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.

淤浪 2024-11-11 17:43:10

关于问题#2 - 将菜单放在单独的方法中。使用一个循环,它的条件是菜单或类似的东西,根据菜单的结果进行处理(这是抽象的,我想你可以从这里弄清楚):

while(doAnotherLoop)
{
    switch(showMenu())
    {
    case 1:
     ...
    case 2:
     ...
    case 5: // Exit
        doAnotherLoop = false;
    }
}

关于问题#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):

while(doAnotherLoop)
{
    switch(showMenu())
    {
    case 1:
     ...
    case 2:
     ...
    case 5: // Exit
        doAnotherLoop = false;
    }
}

Regarding issue #3. You read an int: menuNum = keyboard.nextInt(); but the line is not over, so the next nextLine (String stuName = keyboard.nextLine();) takes the rest of the line. use nextLine() and parse the integers instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文