編輯我應該將每個變數的 setter 方法放在我的 main 方法中的什么位置?
我在java中遇到了一些陣列問題。我正在制作一個應用程式,用戶通過選擇選項 1 將輸入添加到選單中,我正在使用 switch 陳述句。這個陣列需要所有其他 switch 案例都可以訪問,所以我在案例 1 之外宣告了它:
//Menu loop
//variable & array declaration
int myMonths[] = new int[amountOfProjects];
int index = 0;
int num;
while(choice !=6){ //while option 6 is not chosen keep showing the menu
switch (choice){
case 1:
陣列長度是用戶希望在應用程式中存盤的專案數量。對于案例 2,我需要顯示陣列索引的元素/值。如果到目前為止用戶還沒有輸入任何內容,它應該顯示一條訊息說明這一點。這是我的代碼:
case 2:
if(myMonths.length > 0){
// print the values
for(int i=0; i < myMonths.length; i ){
System.out.println(myMonths[i] " ");
}
}else {
System.out.println("No values entered");
}
break;
問題 我的代碼在情況 2 中不會執行 else 陳述句。它只是為用戶分配的陣列長度列印零,所以我猜我只是要求它檢查陣列長度而不是值要素。如果陣列為空,有沒有辦法讓代碼執行錯誤陳述句?
要添加 ,我不確定這是否重要,但我在陣列的單獨檔案上使用了 setter 方法:
public class MenuTestClass{
private int myMonths[];
public MenuTestClass(){
myMonths = new int[5];
}
public MenuTestClass(int[] myMonths){
this.myMonths = myMonths;
}
public void setMyMonths(int[] values){ //declare setter method
myMonths = values;
}
提前致謝!到目前為止,你們都對我很有幫助。
更新:
int myMonths[] = new int[amountOfProjects];
int index = 0;
int num;
while(choice !=6){ //while option 6 is not chosen keep showing the menu
switch (choice){
case 1:
int n = 1; //int n = number of projects
Scanner sc = new Scanner(System.in);
//myMonths = new int[amount];
System.out.println("** Only projects with a duration between 2 and 12 months can be included **");
System.out.println("What was the duration of your projects in months?");
for(int i=0; i<1; i ){
int a = sc.nextInt();
//display error message
if((a < 2) || (a > 12)){
System.out.println(" Please enter an amount between 2 and 12 months. ");}
//add to the array
else{myMonths[index ] = a;}
}
calc.setMyMonths(myMonths); //creating the object
break;
case 2:
if(myMonths.length > 0){
// print the values
for(int i=0; i < myMonths.length; i ){
System.out.println(myMonths[i] " ");
}
} else {
System.out.println("No values entered");
}
break;
case 3:
//display all elements with the same state
//get number user wishes to search for
boolean found = false;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number you wish to search for");
//read user input
num = input.nextInt();
//traverse array
int k = 0;
for(k=0; k < myMonths.length; k ){
if(myMonths[k] == num){
found = true;
System.out.println("Your value has been found at the following indices: " k);
}
}
if(!found){System.out.println("not found");}
break;
//display averages
case 4:
System.out.println("Choice 4 selected\n");
// processing: invoke/call the method calculateAverage() to calculate the average value of the array's elements
calc.calculateAverage();
// output
// use the getter method getAverageCanBeCalculated() to retrieve the value which shows if the average could be calculated
boolean isMenuCalculated = calc.getAverageCanBeCalculated();
if (isMenuCalculated){ // average has been calculated (i.e. there were numbers in the array)
double avg = calc.getAverage();// use the getter method to retrieve the average value
// display the average value
System.out.println("average is: " avg);
}
else { // the instance variables f the
System.out.println("the object's array instance variable has no numbers (i.e. empty array)");
}
break;
case 5:
//invoke/call the method calculateMax() to calculate the maximum element of the array
calc.calculateMax();
// use the getter method to retrieve the maximum value
int max = calc.getMax();
// display the maximum value
System.out.println("maximum is " max);
break;
default:
System.out.println("Please enter a valid number");
}
//display menu again
displayMenu();
//Get user choice
choice = in.nextInt();
}
uj5u.com熱心網友回復:
您可以通過三種方式處理此問題,
使用 apache 庫
匯入 org.apache.commons.lang.ArrayUtils;
ArrayUtils.isEmpty(myMonths);
2)。每當將值輸入陣列時,使用布爾標志并將其設定為真。這樣您就可以檢查該標志。
3)。遍歷陣列,檢查陣列中的值是否不為0。
如果您需要進一步的幫助,請告訴我。
uj5u.com熱心網友回復:
您確定這些值正在更新嗎?根據發布的代碼,“while(”回圈沒有退出條件。似乎也沒有辦法完成回圈。
我建議在 while 回圈的開頭添加一個列印輸出/斷點,以查看您的“選擇”是否實際更新。
...
while (choice != 6) { // while option 6 is not chosen keep showing the menu
System.out.println(choice);
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457664.html
上一篇:如何檢查用戶輸入是否為字串
