Java更改提示顺序

时间:2015-11-11 18:44:32

标签: java

我目前正在开发一个java程序,它与课程和每个课程的学分数量有关。我有一切设置我需要它,除了订单。

我想要一个课程,然后课程有多少学分,然后要求下一个课程,以及那些学分,等等。现在,它将要求所有课程,然后是所有学分。这是我的代码:



//Jake Petersen
import java.util.Scanner;
public class test1{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.println("How many courses are you going to list?");
    int courses = Integer.parseInt(scan.nextLine());
    String courseArray[] = new String[courses];
    for (int i = 0; i < courseArray.length; i++){
      System.out.println("Please enter a course:");
      courseArray[i] = scan.nextLine();
    }
    int creditArray[] = new int[courses];
    for (int i = 0; i < creditArray.length;) {
      System.out.println("Please enter how many credits "+ courseArray[i] + " is:");
      int input = scan.nextInt();
      if (input >= 1 && input <= 4) {
        creditArray[i++] = input; 
      }
    }
    int sum = 0;
    for (int i : creditArray){
      sum += i;
    }
    for (int i = 0; i < courseArray.length; i++) {
      System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
    }
    print(sum);
  }
  public static void print(int sum){
    if(sum >= 12 && sum <= 18){
      System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
    }else if(sum < 12){
      System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
    }else{
      System.out.println("You are taking " + sum + " total credits, which means you are overloaded"); 
    }
  }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

在单个for循环中执行所有提示:

import java.util.Scanner;
public class test1{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.print("How many courses are you going to list?");
        int courses = Integer.parseInt(scan.nextLine());
        String courseArray[] = new String[courses];
        int creditArray[] = new int[courses];
        for (int i = 0; i < courseArray.length; i++){
            System.out.print("Please enter a course:");
            courseArray[i] = scan.nextLine();
            System.out.print("Please enter how many credits "+ courseArray[i] + " is:");
            String credits = scan.nextLine();
            int input = Integer.parseInt(credits);
            if (input >= 1 && input <= 4) {
                creditArray[i] = input;
            }
            else {
                creditArray[i] = 0;
            }
        }    int sum = 0;
        for (int i : creditArray){
            sum += i;
        }
        for (int i = 0; i < courseArray.length; i++) {
            System.out.print(courseArray[i] + " is a " + creditArray[i] + " credit class. \n");
        }
        print(sum);
    }
    public static void print(int sum){
        if(sum >= 12 && sum <= 18){
            System.out.println("You are taking " + sum + " total credits, which makes you a full time student.");
        }else if(sum < 12){
            System.out.println("You are taking " + sum + " total credits, which makes you not a full time student.");
        }else{
            System.out.println("You are taking " + sum + " total credits, which means you are overloaded");
        }
    }
}

当然,这假设2个阵列具有相同的大小。也许你想首先提示一个类计数,知道制作数组有多大,或者动态增长它们。