每次循环再次出现时,我可以使用我的do while循环从我的伪随机数创建一个新数字吗?如果是这样,怎么样?'
编辑:对不起,这是用C++编写的
EDIT2:每次do ... while循环遍历整数时,我只想要一个0到3(0,1,2,3)之间的新数字
我提示用户输入有关汽车的数据.我正在使用的Do ... while()循环第一次正常工作,然后在第一次之后无法正常工作.代码如下,我正在使用Dev-C++.谢谢你的帮助和时间.
#include <iostream>
#include<conio.h>
#include<cstring>
#include<fstream>
#include <iomanip.h>
using namespace std;
int main()
{
char manufacturer[16], model[16], year[10], miles[10], car_cost[12];
char response;
ofstream OS ("usedcars.txt", ios::out);
cout<<"for each car please enter :"<<endl;
do
{
ofstream OS ("usedcars.txt", ios::app);
cout<<"The manufacturer: ";
cin.getline(manufacturer, 16);
cout<<"The model: ";
cin.getline(model, 16);
cout<<"The year: ";
cin.getline(year, 8);
cout<<"The miles: ";
cin.getline(miles, 8);
cout<<"The cost of car $: ";
cin.getline(car_cost, 10);
OS << manufacturer << setw(9) << model << setw(8) << year << …Run Code Online (Sandbox Code Playgroud) 我while在java中的语句遇到问题.这是我的代码(我对此很新)...
import java.text.DecimalFormat;
import java.util.Scanner;
public class ElecticBillLab6 {
public static void main(String[] args) {
int accountNumber;
int customerName;
int oldMeterReading;
int newMeterReading;
int usage;
double charge = 0;
String name;
String lastName;
String response;
final double baseCharge = 5.00;
int yes = 1;
int no = 2;
boolean YesResponse;
Scanner keyBoard = new Scanner(System.in);
do {
System.out.print("Customer's first Name? ");
name = keyBoard.next();
System.out.print("Customer last name?");
lastName = keyBoard.next();
System.out.print("Enter customer account number ");
accountNumber = keyBoard.nextInt(); …Run Code Online (Sandbox Code Playgroud) 所以我正在为一个作业制作一个程序,目前我正在尝试获取用户输入并尝试使其适合某种形式.
例如,用户输入格式为mm/yyyy的日期.如果用户没有以该格式键入,则只会重新提示用户.
到目前为止这是我的小循环:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int findSlash(char[], int);
int findLastEle(char[]);
int main() {
//variables
char expDate[7];
int expLast, expEle;
int expLen;
do{
cout << "Enter experation (form mm/yyyy): ";
cin.getline(expDate, 7);
expLen = strlen(expDate);
expEle = findSlash(expDate, expLen);
expLast = findLastEle(expDate);
}while(expEle != (expLast - 4));
return 0;
}
//findSlash function (finds slash place in array)
int findSlash(char array[], int arrayLen){
int r, dec = 0;
for(r = 0; r < arrayLen; r++){ …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
int i = 0;
do {
i++;
printf("In while loop\n");
} while (i < 3);
}
Run Code Online (Sandbox Code Playgroud)
输出:
In while loop
In while loop
In while loop
Run Code Online (Sandbox Code Playgroud)
为什么printf语句被执行三次?一旦循环开始,i的值变为1,所以循环应该只运行2次但它运行3次,怎么样?
public class Main {
public static void main(String [] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i==3);
}
}
// Outputs 0
Run Code Online (Sandbox Code Playgroud)
为什么迭代停止在零?while(i==3)是一个条件,它测试相等的ito 3.不过,即使增加的值之后i为i++为什么输出仅仅是0?
我有这个代码,这导致我错误.代码遍历列表并查看是否有任何用户登录,如果已登录,则计算它是哪个用户 - 第一个或第二个.
当我将break语句放在if中时,循环只发现了第一个用户并且没有对第二个用户做任何事情 - 整个循环结束了!怎么会这样?我认为中断仅适用于它所在的立即块,即如果在{}中有中断,则它会突破,如果代码执行继续.
break语句是否会结束外部循环?
do {
if (userlogin){
if(how_many_logged_in == 0){
name1.setVisibility(View.INVISIBLE);
}
else if(how_many_logged_in == 1){
name2.setVisibility(View.INVISIBLE);
break ; //this is confusing - where does it break from?
}
}
} while (condition);
Run Code Online (Sandbox Code Playgroud) 我在下面的代码中遇到了一些问题.我还在学习,我不知道如何修复它.
1.我想要做的是创建一个方法(GetInt)来存储变量,就像我想要在第二个方法(GetTrack)中那样进入我的main方法.
2.-当无效输入时我无法获取GetInt方法循环,我猜测try/catch和boolean thingy有问题
谢谢
//Get int Method
static public void GetInt(string sPrompt, int iMin, int iMax)
{
int iNum;
bool bError = false;
do
{
bError = true;
try
{
Console.Write(sPrompt);
iNum = int.Parse(Console.ReadLine());
if ((iNum < iMin) || (iNum > iMax))
{
Console.WriteLine("The value is out of range.");
bError = true;
}
}
catch (ArgumentException)
{
Console.WriteLine("An invalid number was entered, please try again.");
bError = true;
}
}
while (bError == false);
}
//Get Track Method
static …Run Code Online (Sandbox Code Playgroud) 我对使用||的行为感到很困惑 .equals函数的运算符.有没有理由我不能在字符串或其他东西上使用它?
这工作:
do{
System.out.println("Play again? [Y/N]");
//input: Y
play = in.nextLine();
play = play.toUpperCase();
}
while(!"Y".equals(input) ); //breaks out of loop (as it should)
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?!
do{
System.out.println("Play again? [Y/N]");
//input: Y
play = in.nextLine();
play = play.toUpperCase();
}
while( !"Y".equals(input) || !"N".equals(input) ); //infinite loop
Run Code Online (Sandbox Code Playgroud) 我当前的项目要求使用do-while循环和单独的值生成函数的程序.该程序要求输入无限量的输入,直到输入的最后两个数字之间的差值大于12.代码可以工作,但只有当它看起来很明显时.有时它会识别bool已经立即遇到并终止,有时我可以输入15个以上的数字直到它结束,如果它甚至在第一个位置结束.如果可能的话,我想保持我的代码与我目前的相似.我知道我需要一个先决条件,因为第一个输入没有什么可比较的,我正试图弄清楚这一点.
到目前为止这是我的代码:
bool TwelveApart(int x, int y);
int main()
{
int num1, num2;
cout << "Enter some integers: " << endl;
do
{
cin >> num1 >> num2;
TwelveApart(num1, num2);
} while (TwelveApart(num1, num2) == false);
cout << "The difference between " << num1 << " and " << num2 << " is greater than or equal to 12." << endl;
return 0;
}
bool TwelveApart(int x, int y)
{
if (abs(x - y >= 12))
{
return true;
} …Run Code Online (Sandbox Code Playgroud)