数组和打印语句/ Java

时间:2018-08-19 10:42:02

标签: java

我需要制作一个程序,要求用户输入所有者今天使用的卡车数量,无论卡车是“大”还是“小”,并基本打印出卡车的容量。如果用户在使用else语句后没有输入大或小,我就会遇到问题。教授希望我们请用户再试一次。另外,我对如何打印结果感到困惑,尤其是当用户输入超过2辆卡车时。

这是我的代码:

import java.util.Arrays;
import java.util.Scanner;

public class TomTrucking {


/* 
* Complete here if you are using class variables      
*/

    public static void main(String[]args){

        Scanner input = new Scanner(System.in);

        System.out.println("How many trucks are operating today (Number of trucks must be 2 or greater)? :");
        int t1 = input.nextInt();


        if(t1 < 2) {
            System.out.println("You entered a value less than 2 for number of trucks.");
            System.out.println("Terminating program. ");
            System.exit(0);
        }else {
            double crates = 0;
            double crates1 = 0;

            String truck;
            int i=0;

            double array[] = new double[t1];
            double array1[] = new double[t1];
            for( i = 0; i < array.length; i++ ) {

                System.out.println("What is the size of truck " + (i+1) + "(large trucks max crates = 100, small trucks max crates = 10)? ");
                truck = input.next();

                while(!true) {
                    if(truck.equals("large")) {
                        System.out.println("What is the actual number of crates that truck " + (i+1) + " is hauling today (Truck 1 max crates is 100)? ");
                        crates = input.nextDouble();

                        boolean ok = true;
                        array[i] = (crates/100);


                    }else if(truck.equals("small")){
                        System.out.println("What is the actual number of crates that truck " + (i+1) + " is hauling today (Truck 2 max crates is 10)? ");
                        crates1 = input.nextDouble();

                        boolean ok = true;
                        array1[i] = (crates1/10)*100;


                    }else {
                        System.out.println("Enter either large or small: ");

                        boolean ok = false;


                    }
                }




                System.out.println("**Entry for all trucks completed**");    




                System.out.println("Truck " + (i-1) + " max: 100 actual: " + crates + " capacity at: " + (array[i-2]) + "%");
                System.out.println("Truck " + (i) + " max: 10 actual: " + crates1 + " capacity at: " + (array1[i-2]) + "%");

            }
        }
    }    
}

4 个答案:

答案 0 :(得分:3)

首先,while循环的条件是

while(!true) {

这意味着循环内的代码将永远不会处理。您可能是说

while(!ok) {

这意味着您必须在循环外部声明ok

秒,请勿使用System.exit()。这被认为是不良做法。使用布尔标志来控制流。

第三,我建议您使用适当的缩进格式重新设置代码,并使用有意义的变量名称(例如arrayarray1)。这将帮助您发现错误并帮助我们理解代码。另外,将代码分解为逻辑单元,然后将其放入单独的方法中。例如,一种方法处理用户输入,另一种方法验证用户输入,另一种用于计算,最后一种用于显示结果。

答案 1 :(得分:0)

我发现了一些其他问题,您可以尝试

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many trucks are operating today (Number of trucks must be 2 or greater)? :");
    int totalTrucks = input.nextInt();

    if (totalTrucks < 2) {
        System.out.println("You entered a value less than 2 for number of trucks.");
        System.out.println("Terminating program. ");
        System.exit(0);
    } else {
        HashMap<Integer, Double> smallTrucks = new HashMap();
        HashMap<Integer, Double> largeTrucks = new HashMap();

        for (int i = 0; i < totalTrucks; i++) {

            System.out.println("What is the size of truck " + (i + 1) + "(large trucks max crates = 100, small trucks max crates = 10)? ");
            String truckSize = input.next();
            boolean repeatLoop = true;

            while (repeatLoop) {
                if (truckSize.equals("large")) {
                    System.out.println("What is the actual number of crates that truck " + (i + 1) + " is hauling today (Truck 1 max crates is 100)? ");
                    Double crates = input.nextDouble();
                    largeTrucks.put(i, crates);
                    repeatLoop = false;
                } else if (truckSize.equals("small")) {
                    System.out.println("What is the actual number of crates that truck " + (i + 1) + " is hauling today (Truck 2 max crates is 10)? ");

                    boolean sizeWrong=true;
                    while (sizeWrong) {
                        Double crates = input.nextDouble();
                        if (crates > 10) {
                            System.out.println("Small Truck max crates is 10,Please enter size again ");
                            repeatLoop = true;
                            continue;
                        }
                        sizeWrong=false;
                        smallTrucks.put(i, crates);

                    }
                    repeatLoop = false;
                } else {
                    System.out.println("Enter either large or small: ");
                }
            }
        }
        System.out.println("**Entry for all trucks completed**");
        for (int i = 0; i < totalTrucks; i++) {
            Double maxTruckSize = smallTrucks.get(i);

            if (maxTruckSize != null) {
                System.out.println("Truck " + (i + 1) + " max: 100 actual: " + maxTruckSize + " capacity at: " + maxTruckSize / 100 + "%");
            }else
                System.out.println("Truck " + (i + 1) + " max: 10 actual: " + smallTrucks.get(i)+ " capacity at: " + maxTruckSize / 10 + "%");

        }
    }
}

答案 2 :(得分:0)

首先要纠正一些错误,然后再将truck = input.next()移入其中。

我更正了您的代码,并添加了正在运行的程序的输出。但是,如果您坚持使用数组,则在数组未充满输入的情况下打印0。所以我不碰那里。我建议您使用ArrayList而不是array。因此,您可以迭代仅存在的项目。

正常流量:

How many trucks are operating today (Number of trucks must be 2 or greater)? :
2
What is the size of truck 1(large trucks max crates = 100, small trucks max crates = 10)? 
large
What is the actual number of crates that truck 1 is hauling today (Truck 1 max crates is 100)? 
50
What is the size of truck 2(large trucks max crates = 100, small trucks max crates = 10)? 
small
What is the actual number of crates that truck 2 is hauling today (Truck 2 max crates is 10)? 
20
**Entry for all trucks completed**
Truck 1 max: 100 actual: 50.0 capacity at: 0.5%
Truck 2 max: 10 actual: 20.0 capacity at: 0.0%

如果用户输入的值与“大”或“小”不同,则重试:

How many trucks are operating today (Number of trucks must be 2 or greater)? :
2
What is the size of truck 1(large trucks max crates = 100, small trucks max crates = 10)? 
xxx
Enter either large or small: 
What is the size of truck 1(large trucks max crates = 100, small trucks max crates = 10)? 
large
What is the actual number of crates that truck 1 is hauling today (Truck 1 max crates is 100)? 
50
What is the size of truck 2(large trucks max crates = 100, small trucks max crates = 10)? 
small
What is the actual number of crates that truck 2 is hauling today (Truck 2 max crates is 10)? 
20
**Entry for all trucks completed**
Truck 1 max: 100 actual: 50.0 capacity at: 0.5%
Truck 2 max: 10 actual: 20.0 capacity at: 0.0%

解决方案:

public static void main(String[]args){
    Scanner input = new Scanner(System.in);

    System.out.println("How many trucks are operating today (Number of trucks must be 2 or greater)? :");
    int t1 = input.nextInt();

    if(t1 < 2) {
        System.out.println("You entered a value less than 2 for number of trucks.");
        System.out.println("Terminating program. ");
        System.exit(0);
    }
    else {
        double crates = 0;
        double crates1 = 0;

        String truck;
        int i = 0;

        double array[] = new double[t1];
        double array1[] = new double[t1];
        for( i = 0; i < array.length; i++ ) {

            boolean ok = false;

            while(!ok) {
                System.out.println("What is the size of truck " + (i+1) + "(large trucks max crates = 100, small trucks max crates = 10)? ");
                truck = input.next();

                if(truck.equals("large")) {
                    System.out.println("What is the actual number of crates that truck " + (i+1) + " is hauling today (Truck 1 max crates is 100)? ");
                    crates = input.nextDouble();

                    ok = true;
                    array[i] = (crates/100);
                }
                else if(truck.equals("small")){
                    System.out.println("What is the actual number of crates that truck " + (i+1) + " is hauling today (Truck 2 max crates is 10)? ");
                    crates1 = input.nextDouble();

                    ok = true;
                    array1[i] = (crates1/10)*100;
                }
                else {
                    System.out.println("Enter either large or small: ");

                    ok = false;
                }
            }
        }

        System.out.println("**Entry for all trucks completed**");

        System.out.println("Truck " + (i-1) + " max: 100 actual: " + crates + " capacity at: " + (array[i-2]) + "%");
        System.out.println("Truck " + (i) + " max: 10 actual: " + crates1 + " capacity at: " + (array1[i-2]) + "%");


    }
}

答案 3 :(得分:0)

我不清楚这个问题。但是,仅阅读您的代码,似乎此代码存在多个问题,其中之一是while(!true)中的代码块将永远不会执行。并且您需要在while循环之外声明boolean ok。此代码仍然存在一些逻辑错误。但是由于这是您的课堂作业,请尝试自己找出其余的内容。

            boolean ok = false;

            while (!ok) {
                truck = input.next();

                if (truck.equals("large")) {
                    System.out.println("What is the actual number of crates that truck " + (i + 1) + " is hauling today (Truck 1 max crates is 100)? ");
                    crates = input.nextDouble();

                    ok = true;
                    array[i] = (crates / 100);


                } else if (truck.equals("small")) {
                    System.out.println("What is the actual number of crates that truck " + (i + 1) + " is hauling today (Truck 2 max crates is 10)? ");
                    crates1 = input.nextDouble();

                    ok = true;
                    array1[i] = (crates1 / 10) * 100;
                } else {
                    System.out.println("Enter either large or small: ");
                }
            }