开发者

inputs and loops

I'm trying to do as the picture shows here:

inputs and loops

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();


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜