我是编程新手,所以认为我是一个很棒的新手.困境:我想在每次回答"是"时重复我的代码.我确实使用"do while循环",因为首先是一个语句,最后应该评估布尔条件.
代码:
import java.util.Scanner;
class whysoserious {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
do{
String go = YES;
int setone = 0;
int settwo = 0;
System.out.println("Enter two numbers: ");
setone = sc.nextInt();
settwo = sc.nextInt();
int set = setone + settwo;
System.out.println("What is " +setone+ " + " +settwo+ " ? ");
int putone = 0;
putone = sc.nextInt();
if (putone == set)
System.out.println("Correct!");
else
System.out.println("Wrong Answer - The correct answer is: "+set+""); …Run Code Online (Sandbox Code Playgroud) 在while while循环中,计算年平均值和年平均低温平均温度和年平均温度的计算不正常.
while (inFile.hasNext()) {
//extract Month name from file
month = inFile.next();
//Remove comma for better presentation
String formatMonth = month.replace(',', ' ');
//calculate high temperature from text file
highTemp = inFile.nextDouble();
//extract low temperature file from text file
lowTemp = inFile.nextDouble();
//Calculate averaget Temperature from values
averageTemp = (highTemp + lowTemp)/2;
//Calculate Range
range = (highTemp - lowTemp);
//Calculate low temperature
minTemp = Math.min(minTemp, lowTemp);
//Calculate high temperature
maxTemp = Math.max(maxTemp, highTemp);
//Sum up high temperatures
highSum =+ …Run Code Online (Sandbox Code Playgroud) 我试图用c ++简单地猜测我的数字游戏,但计算机需要猜测我的数字.但问题是我陷入了这种无限循环.我只是一个初学者,所以这是一个非常基本的程序.
这是我的代码:
int secretNumber = rand() %100 + 1; // random number between 1-100
int tries=0;
int input;
cout <<"typ your number\n";
cin >> input;
do
{
cout <<secretNumber <<endl;
++tries;
if (secretNumber > input)
{
cout <<"To high i guess?\n";
}
else if (secretNumber < input)
{
cout <<"To low I guess?\n";
}
else
{
cout <<"Yes, i got it in " <<tries <<" tries!";
}
}while (input != secretNumber);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 有人可以解释原因吗?当我使用do-while循环时,我总是遇到无限循环.我错过了什么?
package com.assignment2;
public class FooCorporation {
public static void main(String[] args) {
employee(9.50, 55);
employee(8.20, 47);
employee(10.00, 73);
}
public static void employee(double basePay, int hoursWorked) {
double salary = 0;
int overtimeHours = 40;
do {
if (basePay >= 8.00 || hoursWorked > 40 ) {
if (hoursWorked > 40) {
salary = basePay * hoursWorked
* ((hoursWorked - overtimeHours) * 1.5);
} else {
salary = basePay * hoursWorked;
}
}
else
System.out.println("According to law: Base Pay …Run Code Online (Sandbox Code Playgroud) 我的程序应该将数字输出为单词.例如输入10.输出TEN.除了十点之外,我输入的所有其他数字都是正确的.我只需要帮助来解决这个问题.任何帮助将不胜感激.当我输入10时,它显示为空白.
我的代码:
我没有发现do-while循环的任何实际用途.作为研究,我们在第一次执行时无法控制do-while循环,并且可以使用do-while循环完成所有操作,可以使用while或for循环(假设).
我的问题是:是否存在使用do-while循环的情况,或者与for循环和while循环相比,它提供更可靠结果的任何特定情况?
int i=40; //defined
int main()
{
do
{
printf("%d",i++);
}while(5,4,3,2,1,0); //what does this do ?
return 0;
}
**O/P=40**
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚上面代码中while循环的作用.....
我知道while循环执行至少一次.由于O/P=40while循环的条件评估为false但我无法弄清楚如何?
至于我到目前为止在我的课本教科书中读到的,在我班级的模块中以及2个多小时的搜索中,我无法弄清楚为什么我的代码不起作用.main方法中的do while循环正常工作,但get方法中的do while循环不循环.我输入错误的数字,我收到错误消息,然后再次询问该号码,它继续前进到下一个get方法.
我希望这是一件简单的事情,我忽略了,但我很感激我能得到的任何帮助.
这是我的getHome方法的代码:
public static int getHome()
{
int homeNum;
String home;
do
{
home = JOptionPane.showInputDialog(null,"Enter 1(apartment), 2(house),"
+ " or 3(dorm).","Dwelling Type", JOptionPane.QUESTION_MESSAGE);
homeNum = Integer.parseInt(home);
if(!(homeNum == 1) && !(homeNum == 2) && !(homeNum == 3))
{
JOptionPane.showMessageDialog(null, "The value for dwelling type "
+ "must be 1(apartment), 2(house), or 3(dorm)", "Dwelling"
+ "Type Error", JOptionPane.ERROR_MESSAGE);
}
return homeNum;
}
while(homeNum < 0 || homeNum > 3);
Run Code Online (Sandbox Code Playgroud)
以及调用此方法的main方法中的代码:
public static void main(String[] args)
{
String …Run Code Online (Sandbox Code Playgroud) 我一直试图修复这个循环,但显然它无法识别输入是否正确.我在这里使用正确的循环吗?
var userChoice = prompt("Do you choose rock, paper or scissors?")
if (userChoice !== "rock", "paper", "scissors") {
do {
prompt("Invalid answer. Please choose rock, paper or scissors.");
} while (userChoice !== "rock", "paper", "scissors");
} else {
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
int main()
{
int count=0;
char c=127;
do
count++;
while(c++);
printf("count=%d ",count);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释为什么在第一次循环之后,c的值将变为-128?
do-while ×10
java ×6
c ×2
loops ×2
while-loop ×2
c++ ×1
char ×1
do-loops ×1
javascript ×1
methods ×1