我无法完全理解this。我目前正在研究如何做到这一点,而且,我希望打印出前11个结果(1,2,4,7,11,16,22,29,37,46,56)。
请解释并帮助我做到这一点?
注意:我正在使用do
/ while
循环
import java.util.Scanner;
import java.io.*;
public class DoWhileLoop {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N;
System.out.print("Enter a number ");
N = in.nextInt();
int cut0 = 1;
int totalpieces = 1;
int Ncut = cutN+((cutN-1)+N); // help me here pls
do {
totalpieces = totalpieces + N; // not sure how to construct this too
totalpieces++;
}
while(totalpieces<=56);
System.out.println(totalpieces + " "); // I would want the 1st 11 outcome to be printed (1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56)
}
}
答案 0 :(得分:0)
尝试这样的事情:
String result = "";
Scanner in = new Scanner(System.in);
System.out.print("Enter a number ");
int numberOfCuts = in.nextInt();
int pieces = 1;
int cutCount = 1;
do {
result += " " + pieces;
pieces += cutCount;
cutCount++;
}
while (cutCount <= numberOfCuts);
System.out.println(result);
答案 1 :(得分:0)
试试这个:
Scanner in = new Scanner(System.in);
int N, cut;
System.out.print("Enter a number ");
N = in.nextInt();
do
{
cut = 1+(N*(N+1))/2;
N--;
}while(N == 0);
System.out.println("Total cut: " + cut);
输入N = 5预期输出:16它打印:
输入数字5
16