gpt4 book ai didi

java - 如何修复我的子类循环,使其在主类中调用时仅循环一次?

转载 作者:行者123 更新时间:2023-12-02 06:05:02 24 4
gpt4 key购买 nike

运行程序(在 NetBeans 8.2 中构建和运行)时,它只会运行一次子类循环,而它应该运行 3 次,每个用户输入的变量值运行一次。

我尝试调整 m[0].length,将其替换为:m.length、>4、>3、=>3、=>4,按照 NetBeans 建议转换 for 循环:for (double[] m1:m)。

public class ConversionRate {

public static void main(String[] args) {

//scanner for user input for variables
Scanner input = new Scanner(System.in);

//declare variables to be used
String currency;
double [][] m = new double[3][3];

//user input for starting rate to be converted
System.out.print("Enter USD or EURO as the starting currency to be " +
"converted: ");
currency = input.nextLine();

System.out.println("Enter " + m.length + " values to be converted, " +
"adding a space between each value: ");

for (int i = 0; i < m.length; i++){
for (int j = 0; j == 0; j++){
m[i][j] = input.nextDouble();
}
}
if (currency.equalsIgnoreCase("USD")){
for (int k = 0; k < m.length; k++){
System.out.println("Converted Value " +
" Conversion Rate");
System.out.println("=========================================");
System.out.println(" " + convertedUs(m,k) +
" " + conversionRateUs(m,k));
}
}
else if (currency.equalsIgnoreCase("Euro")){
for (int k = 0; k < m.length; k++){
System.out.println("Starting Value Converted Value " +
" Conversion Rate");
System.out.println("=========================================" +
"==================");
System.out.println(" " + convertedEuro(m,k) +
" " + conversionRateEuro(m,k));
}
} else {
System.out.println("Invalid selection, please try again.");
}
}
public static double conversionRateUs (double [][] m, int columnIndex){

double conversionRateUs = 0.02;
double value2 = 0.00;

for (int i = 0; i < m[0].length; i++){
value2 = m[i][columnIndex] * (0.02 * 100) / 100;
}
return value2;

}
public static double conversionRateEuro (double [][] m, int columnIndex){

double conversionRateEuro = 0.08;
double value2 = 0.00;

for (int i = 0; i < m[0].length; i++){
value2 = m[i][columnIndex] * (0.08 * 100) / 100;
}
return value2;
}

预期结果将计算总转化率和转化“税”率,以对用户输入的每个值进行收费,目前仅显示第一个值。

最佳答案

我确实解决了我的问题,当我只需要使用一维数组时,我试图使用多维数组。现在正在处理信息格式,但我得到了计算和输出,最终可以正常工作:

https://gist.github.com/sometowngeek/0eaf0dcec5e7318f24f66347ef3748b1

关于java - 如何修复我的子类循环,使其在主类中调用时仅循环一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55944866/

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