gpt4 book ai didi

java - 在 While 循环中重复方法

转载 作者:行者123 更新时间:2023-12-01 15:02:37 27 4
gpt4 key购买 nike

我似乎无法让我的主要方法正常工作,因为它应该 100% 工作。这很好,但我需要解决循环问题。

我的主要目标是让程序在用户输入任何数字>=1时重复方法pathCalc(),并在用户输入0时结束程序。但是,当用户输入>=1重复程序时,程序会执行重复,但是当它询问用户是否再次重复或退出时,并且用户输入0退出时,程序重复方法pathCalc()而不是退出。

如何在重复方法后使其工作,以便用户输入 >= 1 重复方法或 0 退出?

    import java.util.Scanner;

public class AssignmentArrays{
static int[] data = new int [6];

public static void getIDs(){

Scanner seg = new Scanner(System.in);

int[] data = new int [6];
data[0] = 0;
data[1] = 0;
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;

/* Segment values */



System.out.println("Enter cost for segment 0:");
data[0] = seg.nextInt();

System.out.println("Enter cost for segment 1:");
data[1] = seg.nextInt();

System.out.println("Enter cost for segment 2::");
data[2] = seg.nextInt();

System.out.println("Enter cost for segment 3:");
data[3] = seg.nextInt();

System.out.println("Enter cost for segment 4:");
data[4] = seg.nextInt();

System.out.println("Enter cost for segment 5:");
data[5] = seg.nextInt();

}

public static void pathCalc(){

/* Path inputs */

Scanner node1 = new Scanner(System.in);


int pathCost;

System.out.println("Enter ID of segment 0 of path:");
int node1value = node1.nextInt();

System.out.println("Enter ID of segment 1 of path:");
int node2value = node1.nextInt();

System.out.println("Enter ID of segment 2 of path:");
int node3value = node1.nextInt();

/* Path cost calculation */

pathCost = data[node1value] + data[node2value] + data[node3value];
System.out.println("The cost of the trip is: $" + pathCost);
}

public static void main(String[] args){
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
int choice;
choice = end.nextInt();

while(choice != 0){
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
choice = end.nextInt();
}
}
}

最佳答案

您在 while 循环中读取了两次选择。如下所示移出一个实例:

    getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
int choice = end.nextInt();
while(choice != 0){
//getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"+
" to evaluate another path:");
choice = end.nextInt();
}

此外,不需要额外的标志。您可以在上述情况下使用选择本身。

如果用户在开头输入 0,则 while 循环将不会启动;当用户随后输入0时,循环将自动终止。

编辑:更新了程序。

public class AssignmentArrays {
static int[] data = new int[6];
static Scanner seg;

public static void getIDs() {
int[] data = new int[6];
data[0] = 0;
data[1] = 0;
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;

/* Segment values */

System.out.println("Enter cost for segment 0:");
data[0] = seg.nextInt();

System.out.println("Enter cost for segment 1:");
data[1] = seg.nextInt();

System.out.println("Enter cost for segment 2::");
data[2] = seg.nextInt();

System.out.println("Enter cost for segment 3:");
data[3] = seg.nextInt();

System.out.println("Enter cost for segment 4:");
data[4] = seg.nextInt();

System.out.println("Enter cost for segment 5:");
data[5] = seg.nextInt();

}

public static void pathCalc() {
int pathCost;

System.out.println("Enter ID of segment 0 of path:");
int node1value = seg.nextInt();

System.out.println("Enter ID of segment 1 of path:");
int node2value = seg.nextInt();

System.out.println("Enter ID of segment 2 of path:");
int node3value = seg.nextInt();

/* Path cost calculation */

pathCost = data[node1value] + data[node2value] + data[node3value];
System.out.println("The cost of the trip is: $" + pathCost);
}

public static void main(String[] args) {
seg = new Scanner(System.in);
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"
+ " to evaluate another path:");
int choice;
choice = seg.nextInt();

while (choice != 0) {
getIDs();
pathCalc();
System.out.println("Enter 0 to exit or any other number"
+ " to evaluate another path:");
choice = seg.nextInt();
}
seg.close();
}
}

关于java - 在 While 循环中重复方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13428395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com