使用while循环倒数并找到平方根。

huangapple go评论59阅读模式
英文:

Use a while loop to count down numbers and finding the square root

问题

使用 while 循环如何对输入的数字进行平方根倒数计数?

例如,如果我运行代码并将第一个数字设置为 8,它会给我 8、7、6 等数字的平方根。

以下是迄今为止我的代码;目前,我只能得到第一个数字的平方根:

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int start;
    int i;
    System.out.print("输入一个正整数:");
    start = scnr.nextInt();
    if (start < 0) {
        System.out.print("错误,请输入一个正整数");
    } else {
        i = start;
        while (i != 0) {
            double sqrt = Math.sqrt(i);
            System.out.printf("%,4f ", sqrt);
            i--;
        }
    }
}
英文:

How do I use a while loop to count down the input number with the square root?

For example, if I were to run the code and put the first number as 8 it would give me the square root of 8..7..6.. and so on.

Below is my code so far; right now, I only get the square root of the first number:

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int start;
    int i;
    System.out.print(&quot;Enter a positive integer: &quot;);
    start = scnr.nextInt();
    if (start &lt; 0) {
        System.out.print(&quot;Error, enter a positive integer&quot;);
    } else {
        for(i = start; i != 0; i--); {
            double sqrt = Math.sqrt(start);
            start--;
            System.out.printf(&quot;%,4f&quot;, sqrt);
        }
    }
}

答案1

得分: 2

这将实现你想要的功能:

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.print("输入一个正整数:");
        int start = scnr.nextInt();
        if (start < 0) {
            System.out.print("错误,请输入一个正整数");
        } else {
            while (start != 0) {
                double sqrt = Math.sqrt(start);
                System.out.printf("%d: %,4f\n", start, sqrt);
                start--;
            }
        }
    }

输出结果:

输入一个正整数36
36: 6.000000
35: 5.916080
34: 5.830952
33: 5.744563
32: 5.656854
31: 5.567764
30: 5.477226
29: 5.385165
28: 5.291503
27: 5.196152
26: 5.099020
25: 5.000000
24: 4.898979
23: 4.795832
22: 4.690416
21: 4.582576
20: 4.472136
19: 4.358899
18: 4.242641
17: 4.123106
16: 4.000000
15: 3.872983
14: 3.741657
13: 3.605551
12: 3.464102
11: 3.316625
10: 3.162278
9: 3.000000
8: 2.828427
7: 2.645751
6: 2.449490
5: 2.236068
4: 2.000000
3: 1.732051
2: 1.414214
1: 1.000000
英文:

This should do what you want:

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        System.out.print(&quot;Enter a positive integer: &quot;);
        int start = scnr.nextInt();
        if (start &lt; 0) {
            System.out.print(&quot;Error, enter a positive integer&quot;);
        } else {
            while (start !=0) {
                double sqrt = Math.sqrt(start);
                System.out.printf(&quot;%d: %,4f\n&quot;, start, sqrt);
                start--;
            }
        }
    }

Output

Enter a positive integer: 36
36: 6.000000
35: 5.916080
34: 5.830952
33: 5.744563
32: 5.656854
31: 5.567764
30: 5.477226
29: 5.385165
28: 5.291503
27: 5.196152
26: 5.099020
25: 5.000000
24: 4.898979
23: 4.795832
22: 4.690416
21: 4.582576
20: 4.472136
19: 4.358899
18: 4.242641
17: 4.123106
16: 4.000000
15: 3.872983
14: 3.741657
13: 3.605551
12: 3.464102
11: 3.316625
10: 3.162278
9: 3.000000
8: 2.828427
7: 2.645751
6: 2.449490
5: 2.236068
4: 2.000000
3: 1.732051
2: 1.414214
1: 1.000000

答案2

得分: 1

如果您想实现为 while 循环

int i = start;
while(i >= 0){
//您的变量 i 正在递减,而不是 start,所以应该使用 Math.sqrt(i)

double sqrt = Math.sqrt(i);
System.out.println(sqrt);
i--;
}


<details>
<summary>英文:</summary>

If you want to implement as a while loop

int i = start;
while(i >= 0){
//your variable i is decrementing not start, so Math.sqrt(i)

double sqrt = Math.sqrt(i);
System.out.println(sqrt);
i--;
}


</details>



# 答案3
**得分**: 0

这里是你的解决方案,我也修复了缩进问题。在你的 if 语句之后有一个分号,使其变得无效。我按照你的要求实现了一个 while 循环,它会显示数字及其平方根,每次迭代递减1。让我知道这是否有效。

```java
public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int start;
    System.out.print("Enter a positive integer: ");
    start = scnr.nextInt();
    if (start < 0) {
        System.out.print("Error, enter a positive integer");
    }
    while (start > 0) {
        double sqrt = Math.sqrt(start);
        System.out.println(start + " - " + sqrt);
        start--;
    }
}
英文:

Heres a solution for ya and i fixed the indentation as well. There was a semicolon after your if statement making it useless. I implemented a while loop as you requested which will display the number then its square root, decreasing by 1 each iteration. Let me know how this works.

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int start;
    System.out.print(&quot;Enter a positive integer: &quot;);
    start = scnr.nextInt();
    if (start &lt; 0) {
        System.out.print(&quot;Error, enter a positive integer&quot;);
    }
    while (start &gt; 0) {
        double sqrt = Math.sqrt(start);
        System.out.println(start + &quot; - &quot; + sqrt);
        start--;
    }
}

huangapple
  • 本文由 发表于 2020年10月16日 04:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64379208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定