Java自动值

时间:2013-10-17 11:44:18

标签: java for-loop

for(int limit1=1; limit1<=1; limit1++){
System.out.print("Number of criteria (Max: 5): ");
int n = Integer.parseInt(inpt.readLine());
System.out.println();
if (n>5 || n<1){
    System.out.println("Sorry the maximum limit is 5");
    System.out.println("Please try again");
    System.out.println();
    limit1=limit1-1;
    continue;
}
double[] arrayCrit = new double[n];
double[] arrayPerc= new double[n];
String[] arrayCritName = new String[n];
double percentage=0, check=0, cent=0;
for (int i = 0; i < n; i++){
  System.out.print("Criteria("+(i+1)+"): ");
  arrayCritName[i] = inpt.readLine();
for(int limit2=1; limit2<=1; limit2++){
    if(n==1){
        arrayCrit[i] = 100/100;
        System.out.print("Percentage: 100%");
        break;
      }
  System.out.print("Percentage: ");
  arrayCrit[i] = Integer.parseInt(inpt.readLine()); 
      if(arrayCrit[i]>100 || arrayCrit[i]<1){
        System.out.println("Sorry, minimum value is 1 and maximum value is 100");
        System.out.println("Please try again");
        System.out.println();
        limit2=limit2-1;            
      }
        check=check+arrayCrit[i];   

     if(check>100 || check<1){
        System.out.println("User didn't meet the default percentage (100%)");
        System.out.println(check);
        check=check-arrayCrit[i];
        limit2=limit2-1;
        continue;   
}
 arrayCrit[i] = arrayCrit[i]/100;

}

}

您好,我在输入百分比时需要帮助。我想知道最后一个输入是否可以自动输入值。

例如:

  • 标准数量:3
  • 标准(1) - 出勤
  • 百分比:25
  • 标准(2) - 测验
  • 百分比 - 25
  • 标准(3) - 考试
  • 百分比 - 50 <---自动存在,无需输入。

谢谢!

3 个答案:

答案 0 :(得分:1)

更改要打印字符串Percentage的代码并读取它。它应该是这样的......

if(i != n-1)
{
   System.out.print("Percentage: ");
   arrayCrit[i] = Integer.parseInt(inpt.readLine()); 
   if(arrayCrit[i]>100 || arrayCrit[i]<1){
    System.out.println("Sorry, minimum value is 1 and maximum value is 100");
    System.out.println("Please try again");
    System.out.println();
    limit2=limit2-1;            
  }
}
else
{
   System.out.print("Percentage: 50");
}

而不是50表示持有该值的值/变量。

答案 1 :(得分:1)

我清理并修改了你的代码:

- 我更改了for s的重试while

- 我删除了一些未使用的变量。

- 我解决了你的自动值问题。我知道您希望自动值为100减去用户输入的所有先前百分比的总和。由于您要保存check变量,因此当您到达上次执行时(n-1),您只需要打印(100次检查)。

这是代码:

System.out.print("Number of criteria (Max: 5): ");
int n = Integer.parseInt(inpt.readLine());
System.out.println();
while( n>5 || n<1 ){
    System.out.println("Sorry the maximum limit is 5");
    System.out.println("Please try again");
    System.out.println();

    n = Integer.parseInt(inpt.readLine());
}

double[] arrayCrit = new double[n];
String[] arrayCritName = new String[n];
double check=0;

for (int i = 0; i < n; i++){
    System.out.print("Criteria("+(i+1)+"): ");
    arrayCritName[i] = inpt.readLine();

    if(i == n-1){
        arrayCrit[i] = (100-check)/100;
        System.out.print("Percentage: "+(100-check)+"%");
        continue;
    }

    System.out.print("Percentage: ");

    while(true){
        arrayCrit[i] = Integer.parseInt(inpt.readLine()); 

        if(arrayCrit[i]>100 || arrayCrit[i]<1){
            System.out.println("Sorry, minimum value is 1 and maximum value is 100");
            System.out.println("Please try again");
            System.out.println();
            continue;
        } 

        check=check+arrayCrit[i];  

        if(check>100 || check<1){
            System.out.println("User didn't meet the default percentage (100%)");
            System.out.println(check);
            check=check-arrayCrit[i];
            continue;   
        }

        arrayCrit[i] = arrayCrit[i]/100;
        break;

    }
}

答案 2 :(得分:0)

简单的解决方案是说“百分比[默认值:50]”而不是“百分比”,并使用一些处理逻辑来查找用户是否实际输入了不同的数字。

更高级的解决方案是在C中使用一些行编辑库,如GNU readline。对于Java,有JLine