下面的代码应该要求用户进入控制台的开始,然后如果用户点击开始"让我们开始",如果没有,则打印"输入了错误的单词"并且该方法自行循环.
do {
System.out.println("Type start to play");
String word = lineReader.nextLine();
String word2 = ("start");
boolean valid;
valid =(word.equals (word2));
if (valid){
System.out.println("Let's begin");
} else {
System.out.println("Wrong Word Entered");
}
} while (!valid);
Run Code Online (Sandbox Code Playgroud) def transpose(matrix):
n=0
while n < (len(matrix)):
li = []
for sets in matrix:
li.append(sets[0])
n += 1
print(len(matrix))
return li
transpose([[1,2,3],[4,5,6],[7,8,9]])
Run Code Online (Sandbox Code Playgroud)
目前返回[1,4,7]通缉结果[[1,4,7],[2,5,8],[3,6,9]]
目前我尝试在php中创建一个函数,它只读取数组字段的int值并将其与最大值进行比较.
如果达到此最大值,则应将该字段的值设置为零,跳转到数组中的下一个字段并增加存储在其中的int值.
如果该值也达到最大值,则执行与上述相同的操作.
如果上述条件不成立,则只增加数组中的存储值.
我的代码看起来像这样:
if($sign_counter[0] === (count($pw_signs) - 1)){
$counter = 0;
while($sign_counter[$counter] === (count($pw_signs) - 1)){
$sign_counter[$counter] = "0";
$counter++;
}
$sign_counter[$counter]++;
}
else{
$sign_counter[0]++;
}
Run Code Online (Sandbox Code Playgroud)
我已经在我的网站和浏览器上用不同的值多次测试了这部分功能.我还检查了值是否正确存储在数组中和所需的变量中.
多数民众赞成我的阵列如何:
$sign_counter = array("38", "2");
Run Code Online (Sandbox Code Playgroud)
例如:
$ sign_counter数组用于存储int值
(count($ pw_signs) - 1)总是等于38(因为计数数组中有39个字段)
$ counter用于确定数组内的字段位置
现在,如果我将值"38"存储在第一个字段中并且"2"存储在数组的第二个字段中,则代码应该检测到,在第一个字段中达到最大值,将字段的值设置为0,然后跳转到下一个字段并将其值增加1.
而不是我想要实现的代码只是增加了数组的第一个字段的值.
看起来while循环只是忽略了它自己的条件,但值本身似乎不是问题.
我真的不明白为什么while循环的行为是这样的.
我完全错过了什么吗?
非常感谢任何帮助.
问候困了
我有一个短暂的循环,如:
boolean a = false;
MyClass b = new MyClass();
b.b = false;
// b is used in other thread...
while(!a){
if(b.b){
throw new Exception("b is true");
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,a永远不会成为现实,但在一些运行之后,布尔变量b.b应该变为真.奇怪的是,while循环从未离开,我的程序在无限循环中发挥作用.
我想知道为什么并决定System.out.print在循环中添加一个语句:
while(!a){
if(b.b){
throw new Exception("b is true");
}
System.out.println("there is something more to execute");
}
Run Code Online (Sandbox Code Playgroud)
值得注意的是我的代码应该是这样的.while循环遍历if语句,直到b.b为true,并抛出异常离开循环.
可能是在第一种情况下,程序停止检查if语句,因为编译器认为没有必要再次检查?如果没有,有人可以向我解释,为什么第一个案例不起作用,但第二个案件不起作用?
我写了一个小程序,只需要通过查看电子邮件地址并将字母返回给字符来获取电子邮件发件人的姓名@.
我用两种方式编写了while循环,一种方式是循环,第二种方式是使用嵌套的if语句.两者是一样好还是一种优于另一种的首选方法?
循环启动时:
public void FindUserName() {
Scanner keyboard = new Scanner(System.in);
char symbol = ' ';
System.out.println("Please enter email!");
symbol = keyboard.findWithinHorizon(".", 0).charAt(0);
while (symbol != '@') {
System.out.print(symbol);
symbol = keyboard.findWithinHorizon(".", 0).charAt(0);
}
System.out.println();
}
Run Code Online (Sandbox Code Playgroud)
使用嵌套的if语句来破坏if symbol == '@'
public void FindUserName() {
Scanner keyboard = new Scanner(System.in);
char symbol = ' ';
System.out.println("Please enter email!");
while (symbol != '@') {
symbol = keyboard.findWithinHorizon(".", 0).charAt(0);
if (symbol == '@') {
break;
}
System.out.print(symbol);
}
System.out.println();
}
Run Code Online (Sandbox Code Playgroud) if (sum < 6) {
System.out.println("You win");
System.out.println();
// Does the user want to retry?
System.out.print("Would you like to retry?(Y or N) : ");
String retry = input.nextLine();
while (true) {
// If they say y or Y, roll again
if (("y".equals(retry)) || ("Y".equals(retry))) {
roll();
// Check for anything other than y and Y
} else if (("n".equals(retry)) || ("N".equals(retry))) {
System.out.println("Closing");
break;
} else if (!("y".equals(retry)) || !("Y".equals(retry))) {
System.out.print("Invalid input. Would you like to retry?(Y or N) …Run Code Online (Sandbox Code Playgroud) 我有这个非常简单的代码:
public class ArrayIO {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number from 50 to 150: ");
int elements = sc.nextInt();
while(elements<50 && elements>150) {
System.out.println("Less than 50 or more than 150");
elements = sc.nextInt();
}
sc.close();
ArrayList<Integer> integers = new ArrayList<>();
for(int i=1; i<=elements; i++) {
integers.add(i);
}
integers.toArray();
System.out.printf("Array size is %d\n", integers.size());
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我的程序有多个条件时,我的程序会完全绕过while循环.它只适用于一种情况.我觉得我正在做一些非常愚蠢的错误,但我已经差不多一个小时了.
我如何使用c#over {}中的var,如:
if
{
var test;
while
{
test = "12345";
//test is defined
}
var test2 = test;
//test is undefined
}
Run Code Online (Sandbox Code Playgroud)
我不明白.
我不明白这段代码是如何工作的:
i = 1
while False:
if i % 5 == 0:
break
i = i + 2
print(i)
Run Code Online (Sandbox Code Playgroud)
什么呢while False?什么是假的?我不明白......
我想知道是否有人可以帮助我.我是编程新手,这是我第一次使用stackoverflow.
我正在寻找创建一个while循环,询问用户的背景,一旦输入正确,就可以打破循环.我可以用break来退出,但我试图找到另一种方式.到目前为止我的代码是:
var background = "";
while (background != "M" || background != "W" || background != "R")
{
Console.WriteLine("Welcome " + name + ", " + "Please pick a class: \n" +
"(M)age \n" +
"(W)arrior \n" +
"(R)ogue \n");
var readLine = Console.ReadLine();
if (readLine != null) background = readLine.ToUpper();
if (background == "M")
{
Console.WriteLine("Welcome Mage " + name);
}
else if (background == "W")
{
Console.WriteLine("Welcome Warrior " + name);
}
else if (background == "R") …Run Code Online (Sandbox Code Playgroud) while-loop ×10
java ×5
c# ×2
if-statement ×2
python ×2
arrays ×1
eclipse ×1
function ×1
loops ×1
methods ×1
nested-if ×1
php ×1
python-3.x ×1
var ×1