我已經經過了無數次的論壇,發現了幾個答案,非常有幫助。我是Java新手,所以要溫柔。基本問題是我有兩個類Main和一個方法容器。我需要從用戶的命令行輸入,要求用戶設置數組的大小。然後,用戶可以在陣列中的每個元素設置爲一個特定的值,它必須是有選項來設置這些值,即輸入2.然後再選擇包括獲得最小值,最大值,平均值等..傳遞裏面的參數if-else
我我一直在努力,直到我檢查用戶輸入時,代碼才工作,使用if-else或switch,因爲每個代碼塊都沒有看到創建的變量值。例如,設置數組大小參數不會傳遞到設置每個元素值的方法中。任何建議都會很棒。下面是代碼:以下
import java.util.Scanner;
public class fc {
int command;
public void commandline() {
Scanner scan = new Scanner(System.in);
int command;
System.out.print("Enter command>");
command = scan.nextInt();
while (command!=9)
{
if(command==1){ //set the array size
int arraySize=getArraySize();
}else if (command==2){ //enter values for the array
int[]arrValues=setArrayValues(arraySize);
}else if (command==3){
int [] theNumbers=arrayPrint(arrValues);
arrayPrint(arrValues);
}else if (command==4){
sumArray(arrValues);
System.out.println("The sum of the array is ");
}else if (command==5){
minArray(arrValues);
}else if (command==6){
maxArray(arrValues);
}else if (command==7){
avgArray(arrValues);
}else{
printMenu();
}
//Read in next command
System.out.print("Enter command>");
command = scan.nextInt();
}
}
public static int[] arrayPrint(int[] arrayValues) {
System.out.println("Elem Value");
for(int i=0;i<arrayValues.length;i++){
System.out.printf("%s\t%d\n", i,arrayValues[i]);
}
return arrayValues;
}
public static int avgArray(int[] arrayValues) {
int[] theNumbers1=arrayValues;
int theSum=0;
int avg=0;
for (int theCounter=0; theCounter <theNumbers1.length; theCounter++)
theSum+=theNumbers1[theCounter];
avg=theSum/theNumbers1.length;
return avg;
}
public static int maxArray(int[] arrayValues) {
int max = arrayValues[0];
for (int i = 0; i < arrayValues.length; i++) {
if (arrayValues[i] > max) {
max = arrayValues[i];
}
}
return max;
}
public static int minArray(int[] arrayValues) {
int min = arrayValues[0];
for (int i = 0; i < arrayValues.length; i++) {
if (arrayValues[i] < min) {
min = arrayValues[i];
}
}
return min;
}
public static int sumArray(int[] arrayValues) {
int[] theNumbers1=arrayValues;
int theSum=0;
for (int theCounter=0; theCounter <theNumbers1.length; theCounter++)
theSum+=theNumbers1[theCounter];
return theSum;
}
public static int getArraySize() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the array?");
int arraySize = input.nextInt();
return arraySize;
}
public static int[] setArrayValues(int arraySize){
Scanner scan = new Scanner(System.in);
int[] arr = new int[arraySize];
System.out.println("Enter "+arraySize+ " numbers after each number press the enter key: ");
for(int i = 0; i < arr.length; i++){
System.out.println("Set the value for index "+i);
arr[i] = scan.nextInt();
}
return arr;
}
public void printMenu() {
// Print out commands to use
System.out.println("Type '1' to create an array of values");
System.out.println("Type '2' to set a values in the array");
System.out.println("Type '3' to print the array of values");
System.out.println("Type '4' to sum an array of values");
System.out.println("Type '5' to return the minimum number in the an array of values");
System.out.println("Type '6' to return the maximum number in the an array of values");
System.out.println("Type '7' to return the average of the array of values");
}
}
是否所有代碼都與您遇到的特定問題相關? –