do/while 循环懒惰的餐饮服务商的序列
do/while loop lazy caterer's sequence
我无法完全理解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)
}
}
尝试这样的事情:
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);
试试这个:
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 打印:
Enter a number 5
16
我无法完全理解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)
}
}
尝试这样的事情:
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);
试试这个:
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 打印:
Enter a number 5
16