数组和打印语句/java
我需要制作一个程序,要求用户输入车主目前使用的卡车数量,卡车是“大”还是“小”,并基本打印卡车的容量。如果用户在使用else语句后没有输入small或large,我会遇到问题。教授希望我们让用户再试一次。此外,我对如何打印结果感到困惑,尤其是如果用户输入的卡车超过2辆 这是我的密码:数组和打印语句/java,java,Java,我需要制作一个程序,要求用户输入车主目前使用的卡车数量,卡车是“大”还是“小”,并基本打印卡车的容量。如果用户在使用else语句后没有输入small或large,我会遇到问题。教授希望我们让用户再试一次。此外,我对如何打印结果感到困惑,尤其是如果用户输入的卡车超过2辆 这是我的密码: import java.util.Arrays; import java.util.Scanner; public class TomTrucking { /* * Complete here if you
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]) + "%");
}
}
}
}
导入java.util.array;
导入java.util.Scanner;
公营货车运输{
/*
*如果使用类变量,请在此处完成
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“今天有多少辆卡车在运行(卡车数量必须为2辆或更多)?:”;
int t1=input.nextInt();
如果(t1<2){
System.out.println(“您输入的卡车数量值小于2”);
System.out.println(“终止程序”);
系统出口(0);
}否则{
双板条箱=0;
双板条箱1=0;
缆车;
int i=0;
双数组[]=新的双数组[t1];
双阵列1[]=新双阵列[t1];
对于(i=0;i
首先,while循环的条件是
while(!true) {
这意味着循环中的代码永远不会处理。你可能是说
while(!ok) {
这意味着您必须在循环之外声明ok
其次,不要使用System.exit()
。这被认为是坏习惯。使用布尔标志控制流
第三,我建议您使用适当的缩进重新格式化代码,并使用有意义的变量名称(例如
array
和array1
)。这将帮助您发现错误并帮助我们了解代码。此外,将代码分解为逻辑单元,并将它们放入单独的方法中。例如,一种方法处理用户输入,另一种方法验证用户输入,另一种方法用于计算,最后一种方法用于显示结果。首先,while循环的条件是
while(!true) {
这意味着循环中的代码永远不会处理。你可能是说
while(!ok) {
这意味着您必须在循环之外声明ok
其次,不要使用System.exit()
。这被认为是坏习惯。使用布尔标志控制流
第三,我建议您使用适当的缩进重新格式化代码,并使用有意义的变量名称(例如
array
和array1
)。这将帮助您发现错误并帮助我们了解代码。此外,将代码分解为逻辑单元,并将它们放入单独的方法中。例如,一种方法处理用户输入,另一种方法验证用户输入,另一种方法用于计算,最后一种方法用于显示结果。我发现还有一些问题,您可以尝试
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 + "%");
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“今天有多少辆卡车在运行(卡车数量必须为2辆或更多)?:”;
int totalTrucks=input.nextInt();
如果(总卡车数<2){
System.out.println(“您输入的卡车数量值小于2”);
System.out.println(“终止程序”);
系统出口(0);
}否则{
HashMap smallTrucks=新的HashMap();
HashMap largeTrucks=新HashMap();
对于(int i=0;ipublic 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]) + "%");
}
}
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: ");
}
}