gpt4 book ai didi

java - 如何从 do while 循环内部检索变量值

转载 作者:行者123 更新时间:2023-12-01 15:19:08 25 4
gpt4 key购买 nike

我是 Java 初学者,正在为一门类(class)开发 Mastermind 项目。

我有一个问题。我想检索 leftDigit 的值并将其放入数组 guess[4] 中。但我不知道如何在循环之外检索它。

假设当我输入数字 1234 时,我希望它变成 int[]guess = { 1, 2, 3, 4};`

public static void inputNumber(){   

Scanner input = new Scanner(System.in);
currentRow++;

System.out.printf ("Enter 4 numbers for attempt #%d: ", currentRow);

int count = 10000, leftDigit;
double tempNum;

int number = input.nextInt();


//finding the left digit
do{
tempNum = (double) number / count ;
leftDigit = (int) (tempNum * 10) ;
count /=10;
number = number - ( count * leftDigit);
} while (count != 1 );
}

最佳答案

    int [] guess = new int[4];
int i = 0;
//finding the left digit
do{
tempNum = (double) number / count ;
leftDigit = (int) (tempNum * 10) ;

guess[i++] = leftDigit;

count /=10;
number = number - ( count * leftDigit);
} while (count != 1 );

//Use guess as you want it contains the 4 digits you need

关于java - 如何从 do while 循环内部检索变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11214827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com