虽然循环,但似乎无法获得正确的数据

时间:2016-03-01 06:21:14

标签: java debugging while-loop

编写一个要求整数num的类,确保它大于0(数据验证),并计算并返回以下一系列数字的总和:

1/num + 2/num-1 + 3/num-2 + …. (num-1)/2 + num/1

不要陷入整数分裂!

使用num = 2;调用您的方法,您希望结果为2.5

    public class LengthSeries
{
    public static void main (String[] args)
    {
    //variable declarations
    Scanner keyboard =  new Scanner(System.in);
    int num;
    int sum = 0; // sum
    int ctr = 1 ; //counter

    //Accept the required data

    System.out.print ("Enter a whole number greater than 0 :  ");
    num = keyboard.nextInt();



    //Process the data in order to determine data
    while ( num <= 0){
                     System.out.println ("Sorry number invalid");
                     System.out.println ("please write a whole number greater than 0 :  ");
                     num = keyboard.nextInt();
                  }


    while (num <= 0){  

                    sum += (ctr / num);
                    num -= ctr;
                    ctr++;
                } 





    //Display the output
    System.out.println ();
    System.out.println ("Your sum is:  "         +sum);
    System.out.println ("\n\nWritten by: KinnahRose Lopez");

    } //end main method
} //end LengthSeries class

3 个答案:

答案 0 :(得分:0)

你可以试试这个:

public static void main(String[] args) throws Exception {
    Scanner keyboard =  new Scanner(System.in);
    System.out.print ("Enter a whole number greater than 0 :  ");
    int num = keyboard.nextInt();
    while (num<1) {
        System.out.print ("Enter a whole number greater than 0 :  ");
        num = keyboard.nextInt();
    }

    double sum = 0;
    double d = num; // convert num to double to get the correct result.
    for (int i = 1; i <= num; i++) {
        sum += i/(d-(i-1));
    }
    System.out.println("sum = " + String.format("%.2f", sum));
}

示例:

Enter a whole number greater than 0 :  2
sum = 2.50

答案 1 :(得分:0)

import java.util.Scanner;`enter code here`
public class Test
{
    public static void main (String[] args)
    {
        //variable declarations
        Scanner keyboard =  new Scanner(System.in);
        int num;
        double sum = 0; // sum
        //Accept the required data
        System.out.print ("Enter a whole number greater than 0 :  ");
        num = keyboard.nextInt();
        //Process the data in order to determine data
        while ( num <= 0){
            System.out.println ("Sorry number invalid");
            System.out.println ("please write a whole number greater than 0 :  ");
            num = keyboard.nextInt();
        }
        int temp = num;
        for (double i = 1; i <= num; i++) 
        {
            sum = sum + (i/temp);
            temp--;
        } 
        //Display the output
        System.out.println ();
        System.out.println ("Your sum is:  "         +sum);
        System.out.println ("\n\nWritten by: KinnahRose Lopez");

    } //end main method
} //end LengthSeries class

答案 2 :(得分:-1)

double sum =0;
for(double i=1.0; i<= num; i+=1.0) {
    sum+=i/(num-i-1);
}