我如何将 java 中的数字重复乘以 2 直到达到 100 万?
how do i repeatedly multiply a number in java by 2 until it reaches 1 million?
import java.util.Scanner;
class Main {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
int testNumber = userInput.nextInt();
do{
System.out.println(newNumber * 2);
newNumber++;
}while( testNumber < 1000000);
}
}
乘以 2 后需要更新数字:
newNumber = newNumber * 2;
System.out.println(newNumber);
您还使用了 newNumber
和 testNumber
并且 newNumber
似乎没有在任何地方定义...
}while( ***testNumber***newNumber*** < 1000000);
您需要选择一个,因为如果您要更新 newNumber
但在循环中比较 testNumber
,您将创建一个无限循环。
除非您在 post.
中遗漏了某些内容,否则您展示的代码不应编译
你的循环想法是正确的,但你的变量有多个问题。
- 您的第一个问题是您从用户那里读入了一个变量 -
testNumber
,但随后您(错误地)操纵了一个完全 不同的 变量 - newNumber
.
- 你的第二个问题是你正在测试 unchanged 变量作为你的停止条件。
你可能希望你的循环是这样的:
do {
testNumber = testNumber * 2;
System.out.println(testNumber);
} while(testNumber < 1000000);
你也可以为它做一个递归的方法
public int reachMillion(int num) {
if(num<=0)
return -1; // indicating it is not possible.
if(num>=1000000) // Base Condition denoting we have reached 1 million
return num;
return reachMillion(num*2); // recursive part to multiply by 2 until we reach 1 million
}
class Main {
private static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
int newNumber = 0;
do{
System.out.println("Enter a positive number: ");
try{
newNumber = userInput.nextInt();
}catch(Exception ignored){ }
System.out.println("");
}while(newNumber <= 0);
System.out.println("----- " + newNumber + " multiply by 2 ------");
while(newNumber <= 1_000_000){
System.out.print("2 * " + newNumber +" = ");
newNumber <<= 1;//in some compilers left shift is faster than multiply
System.out.println(newNumber);
}
}
描述这里出了什么问题。我想提供一个完整的例子:
import java.util.Scanner;
public class Main {
private static Scanner userInputScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Please input a number: ");
int userInputNumber = userInputScanner.nextInt();
System.out.println();
int newNumber = userInputNumber;
while (newNumber < 1_000_000) {
newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left
System.out.println(newNumber);
}
}
}
Try it online!
当心! 该代码不处理任何错误的用户输入。例如,如果你给它0
,它会永远循环,如果你给它foo
,它会崩溃。如果您想处理用户输入的所有边缘情况,可以这样做:
import java.util.*;
public class Main {
private static Scanner userInputScanner = new Scanner(System.in);
public static void main(String[] args) {
int userInputNumber;
//
while(true) {
System.out.print("Please input a number: ");
if (userInputScanner.hasNext()) {
// The user gave us something, but we don't know if it's a number
String rawUserInput = userInputScanner.next();
try {
userInputNumber = Integer.parseInt(rawUserInput);
// If that previous line runs, the user has given us an integer!
System.out.println();
if (userInputNumber > 0) {
// The user has given a valid number. Break out of the loop and multiply it!
break;
}
else {
// The user has given a bad number. Tell them why and ask again.
System.out.println("The number has to be greater than 0.");
}
}
catch (NumberFormatException exception) {
// The user has given us something, but it wasn't an integer
System.out.println();
System.out.println("That is not a number: " + exception.getMessage());
}
}
else {
// There is no input, so we can't do anything.
return;
}
}
// Done looping through user input
int newNumber = userInputNumber;
while (newNumber < 1_000_000) {
newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left
System.out.println(newNumber);
}
}
}
Try it online!
do-while
循环有一个棘手的部分。在那种类型的循环中, do
部分首先执行。对于下面的示例,虽然输入已经大于 1000000,但它会打印 1000001。
public void doWhileLoop() {
int num = 1000001;
do {
System.out.println(num);
num *= 2;
} while (num < 1000000);
}
因此,在 do-while 循环中执行某些操作之前使用一些保护子句(也称为 if-statements
)是个好主意。喜欢,
public void doWhileLoop() {
int num = 1000001;
if(num >= 1000000) {
return;
}
do {
System.out.println(num);
num *= 2;
} while (num < 1000000);
}
import java.util.Scanner;
class Main {
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
int testNumber = userInput.nextInt();
do{
System.out.println(newNumber * 2);
newNumber++;
}while( testNumber < 1000000);
}
}
乘以 2 后需要更新数字:
newNumber = newNumber * 2;
System.out.println(newNumber);
您还使用了 newNumber
和 testNumber
并且 newNumber
似乎没有在任何地方定义...
}while( ***testNumber***newNumber*** < 1000000);
您需要选择一个,因为如果您要更新 newNumber
但在循环中比较 testNumber
,您将创建一个无限循环。
除非您在 post.
中遗漏了某些内容,否则您展示的代码不应编译你的循环想法是正确的,但你的变量有多个问题。
- 您的第一个问题是您从用户那里读入了一个变量 -
testNumber
,但随后您(错误地)操纵了一个完全 不同的 变量 -newNumber
. - 你的第二个问题是你正在测试 unchanged 变量作为你的停止条件。
你可能希望你的循环是这样的:
do {
testNumber = testNumber * 2;
System.out.println(testNumber);
} while(testNumber < 1000000);
你也可以为它做一个递归的方法
public int reachMillion(int num) {
if(num<=0)
return -1; // indicating it is not possible.
if(num>=1000000) // Base Condition denoting we have reached 1 million
return num;
return reachMillion(num*2); // recursive part to multiply by 2 until we reach 1 million
}
class Main {
private static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
int newNumber = 0;
do{
System.out.println("Enter a positive number: ");
try{
newNumber = userInput.nextInt();
}catch(Exception ignored){ }
System.out.println("");
}while(newNumber <= 0);
System.out.println("----- " + newNumber + " multiply by 2 ------");
while(newNumber <= 1_000_000){
System.out.print("2 * " + newNumber +" = ");
newNumber <<= 1;//in some compilers left shift is faster than multiply
System.out.println(newNumber);
}
}
import java.util.Scanner;
public class Main {
private static Scanner userInputScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Please input a number: ");
int userInputNumber = userInputScanner.nextInt();
System.out.println();
int newNumber = userInputNumber;
while (newNumber < 1_000_000) {
newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left
System.out.println(newNumber);
}
}
}
Try it online!
当心! 该代码不处理任何错误的用户输入。例如,如果你给它0
,它会永远循环,如果你给它foo
,它会崩溃。如果您想处理用户输入的所有边缘情况,可以这样做:
import java.util.*;
public class Main {
private static Scanner userInputScanner = new Scanner(System.in);
public static void main(String[] args) {
int userInputNumber;
//
while(true) {
System.out.print("Please input a number: ");
if (userInputScanner.hasNext()) {
// The user gave us something, but we don't know if it's a number
String rawUserInput = userInputScanner.next();
try {
userInputNumber = Integer.parseInt(rawUserInput);
// If that previous line runs, the user has given us an integer!
System.out.println();
if (userInputNumber > 0) {
// The user has given a valid number. Break out of the loop and multiply it!
break;
}
else {
// The user has given a bad number. Tell them why and ask again.
System.out.println("The number has to be greater than 0.");
}
}
catch (NumberFormatException exception) {
// The user has given us something, but it wasn't an integer
System.out.println();
System.out.println("That is not a number: " + exception.getMessage());
}
}
else {
// There is no input, so we can't do anything.
return;
}
}
// Done looping through user input
int newNumber = userInputNumber;
while (newNumber < 1_000_000) {
newNumber *= 2; // Take the variable on the left, multiply it by the number on the right, and save it in the variable on the left
System.out.println(newNumber);
}
}
}
Try it online!
do-while
循环有一个棘手的部分。在那种类型的循环中, do
部分首先执行。对于下面的示例,虽然输入已经大于 1000000,但它会打印 1000001。
public void doWhileLoop() {
int num = 1000001;
do {
System.out.println(num);
num *= 2;
} while (num < 1000000);
}
因此,在 do-while 循环中执行某些操作之前使用一些保护子句(也称为 if-statements
)是个好主意。喜欢,
public void doWhileLoop() {
int num = 1000001;
if(num >= 1000000) {
return;
}
do {
System.out.println(num);
num *= 2;
} while (num < 1000000);
}