我正在教自己一些C#并且有了构建温度计算器的想法.我想使用do-while循环来确保用户选择有效的菜单选项.我试图做的是:
do
{
//etc etc etc;
} (while menuChoice != 1||2||3);
Run Code Online (Sandbox Code Playgroud)
换句话说,除非menuChoice为1,2或3,否则继续执行循环内部的操作.VS2010 Express告诉我我不能这样做.有没有办法以这种方式做到这一点,还是我必须做其他事情?有关如何执行此操作的建议或是否应使用其他解决方案?
我是编程新手.我想创建一个添加10个整数的程序,并显示一个消息框,指示我得到了多少正确的答案.我只用了一个标签盒和一个按钮.这是我使用的代码.它完美地工作,直到消息框显示我只有1个正确的答案.我究竟做错了什么?谢谢.
}
int a;
int b;
string sagot;
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
do
{
a = i + i;
label1.Text = i.ToString() + " + " + i.ToString() + "=";
sagot = Interaction.InputBox("Please type your answer:");
i++;
} while (i <= 10);
{
if (a == Convert.ToInt32(sagot))
{
b++;
}
}
MessageBox.Show("Number of correct answers: " + b.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在do while函数中运行一些代码:
do {
printf("\nThis game has two different modes: I Guess, You Guess\n");
Sleep(2000);
printf("Which mode do you want to play?\n");
cin >> mStr;
cout << "Are you sure you want to play " << mStr << " mode?";
cin >> choice;
} while (choice != "No");
Run Code Online (Sandbox Code Playgroud)
但是,每次我输入mStr(一个char数组)时,它都会重新启动.它甚至没有执行cout.
以下是名为char的数组:
char mStr[10];
char choice[4];
Run Code Online (Sandbox Code Playgroud)
另外,我怎么能用printf()而不是cout呢?我正努力练习.
编辑:
这是新代码:
do {
printf("\nThis game has two different modes: I Guess, You Guess\n");
Sleep(2000);
printf("Which mode do you want to play?\n");
cin >> mStr; …Run Code Online (Sandbox Code Playgroud) 我正在尝试一个简单的do while循环,假设在输入小于1且大于1000时运行.它应该要求用户在循环中输入正确的数字.它现在似乎正在做的是再一次重复循环,要求输入正确,然后显示结束消息.如果满足条件,不确定为什么重复它
String name = JOptionPane.showInputDialog(null,
"Please enter students lastname");
int input = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please enter students ID"));
do {
JOptionPane.showMessageDialog(null,
"Please enter a student ID within the correct parameters");
input = Integer.parseInt(JOptionPane.showInputDialog(null,
"Please enter students ID"));
} while (input < 1 && input > 1000);
// Output dialog with user input
JOptionPane.showMessageDialog(null, "StudentID: " + input
+ "\nStudent Last: " + name);
Run Code Online (Sandbox Code Playgroud) 我必须为我的课程编写一个程序,该程序将用户输入作为家庭作业,实验室,测试等,将它们放在一个字符串中,并且有一个单独的类将它们分成单独的"双"数字并包含一个writeToFile方法来编写学生的成绩为档案.
我一直遇到问题.当我第一次询问用户他们的名字时,它工作正常,但如果用户决定再次进入do-while循环,则会跳过"你的名字是什么",它会消失直接到id.我知道它与最后一个不是字符串的输入有关,而解决方案就是放一个"keyboard.nextLine();" 在问题之上,但我尝试过,它甚至在询问问题之前只询问用户他们的名字..
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class GradeApplication
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
//Define variables
String name;
int id;
String homework;
String labs;
String tests;
double project;
double discussion;
int answer;
do
{
System.out.print("\nWhat is your name? ");
name=keyboard.nextLine();
System.out.print("\nWhat is your student ID? ");
id=keyboard.nextInt();
homework = keyboard.nextLine();
System.out.println("\nPlease enter homework grades separated by spaces:");
homework = keyboard.nextLine();
System.out.println("Please enter lab grades separated by spaces:");
labs …Run Code Online (Sandbox Code Playgroud) 我正在尝试做一个简单的do while循环,其中除'y'或'n'以外的任何字母都无效并且循环重复.有谁知道为什么这个循环总是评估为假?即使输入了有效字符?
char user_response[2];
do {
printf("\nDo you want to process another range (y or n): ");
scanf("%1s", user_response);
user_response[0] = tolower(user_response[0]);
}
while (user_response[0] != 'y' || user_response[0] != 'n');
return user_response[0];
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用嵌套的if语句执行"do while"循环.我正在尝试比较String变量"word"的两个可能值.如果!word.equals"deeppan或thin"做某事,否则做点什么.但它不喜欢我使用或|| 比较..任何建议都会受到欢迎.
do {
word = scan.next();
if ( !word.equalsIgnoreCase( "Deeppan" || "thin" ) ) {
System.out.print("Sorry you must specify a Deeppan or thin base, try again: ");
} else {
break;
}
} while ( true );
Run Code Online (Sandbox Code Playgroud) 我有以下函数,do while里面有一个循环:
void GetFiveNumericValues()
{
int validationResult;
char input[5];
do
{
printf("Please enter 5 digits:\n");
validationResult = scanf("%s", &input);
printf("Validation result: %d\n", validationResult);
} while (validationResult != 1);
// while (!(validationResult == 1));
// while (validationResult > 1 || validationResult < 1);
}
Run Code Online (Sandbox Code Playgroud)
即使在时,循环也没有完成validationResult == 1.
我在这里错过了什么?
我正在通过Ivor Horton的Beginning Visual C++ 2012工作,我在第三章练习二:决策和循环.这是我的代码:
#include <iostream>
using namespace std;
int main()
{
char letter;
int vowels = 0;
do
{
cout << "Enter a letter, and enter q or Q to end" << endl;
cin >> letter; // Enter a letter
switch (letter)
{
case 'a' || 'A' || 'e' || 'E' || 'i' || 'I' || 'o' || 'O' || 'u' || 'U': // If letter is a vowel, add to vowels variable
vowels++; …Run Code Online (Sandbox Code Playgroud) 此代码用于输入n integer值并计算no.的even和odd值的那些输入值中.
这个java代码显示了ArrayIndexOutOfBoundsException何时使用do..while循环,但是当使用for循环时它工作正常.没有改变任何事情只是重新安排语法,以便将for循环转换为do while循环.
对于循环:
import java.util.*;
public class EvenOddCount
{
public static void main(String args[]) throws Exception
{
System.out.print("Enter the no. of inputs to be taken : ");
int evenCount=0, oddCount=0;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter the inputs : ");
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
evenCount++;
else
oddCount++;
} …Run Code Online (Sandbox Code Playgroud)