如何解决如何在do ... while循环中验证输入,然后在必要时重新提问?
到目前为止,我已经尝试做一个do ... while循环,它询问两个问题。一个是多少英里,第二个是包装重量。如果英里数等于或小于0,则应输出错误,然后重新提问。验证后,它应该转到具有相同要求的权重问题,并且如果权重无效,则应该重复权重问题,因为里程问题已经有效。
这是我到目前为止的代码:
apiInterface = ApiClient.getClient().create(ApiInterface.class);
apiInterface.getTopRatedMovie(API_KEY)
.interval(3,TimeUnit.SECONDS)
.flatMap(n -> apiInterface.getTopRatedMovie(API_KEY))
.repeat()
.subscribe(new Observer<MovieResopnse>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(@NonNull MovieResopnse movieResopnse) {
movies = movieResopnse.getResults();
Log.d(TAG,"onResponse: Number of movies received: " + movies.size());
tv_count.setText("Movie: " + count);
count++;
Picasso.get()
.load(IMAGE_URL + movies.get(item).getPosterPath())
.into(image);
title.setText(movies.get(item).getOriginalTitle());
subtitle.setText(movies.get(item).getTitle());
description.setText(movies.get(item).getOverview());
rating.setText("" + movies.get(item).getVoteAverage());
release_date.setText("Release: " + movies.get(item).getReleaseDate());
}
@Override
public void onError(@NonNull Throwable e) {
Log.d(TAG,"onError: " + e.getMessage());
Toast.makeText(MainActivity.this,"Error: " + e.getMessage(),Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
解决方法
您可以使用多个while循环,每个输入一个。
int miles = 0;
double weight = 0.0;
bool correct = false;
while (!correct)
{
cout << "Enter the number of miles as a whole number: " << std::endl;
bool success = cin >> miles;
if (!success || miles < 0) {
cout << "Invalid miles value -- must be nonnegative." << std::endl;
}
else {
correct = true;
}
}
correct = false;
while (!correct)
{
cout << "Enter the weight in pounds: " << std::endl;
bool success = cin >> weight;
if (!success || weight < 0 || weight > 10) {
cout << "Invalid weight value -- must be between 0 and 10." << std::endl;
}
else {
correct = true;
}
}
// do calculation
cout << "\nGood-bye!\n";
return 0;
,
您需要其他循环才能从cin
读取,直到输入有效为止。并且您需要确保cin
甚至在读取值之前都能成功读取,然后再对该值执行范围检查。
我建议在单独的函数中进行读取+验证,例如:
#include <iostream>
#include <limits>
template<typename T>
T prompt(const char *msg,T maxValue = std::numeric_limits<T>::max())
{
T value;
do
{
cout << msg << ": ";
if (!(cin >> value))
{
cout << "\n\tError: Invalid input!\n" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
else if (value < 0)
{
cout << "\n\tError: Value must be greater than 0!\n" << endl;
}
else if (value >= maxValue)
{
cout << "\n\tError: Value must be less than " << maxValue << "!\n" << endl;
}
else
break;
}
while (true);
return value;
}
...
int miles;
double weight;
int choice;
do
{
miles = prompt<int>("Enter the number of miles as a whole number");
weight = prompt<double>("Enter the weight of the package in pounds",10.0);
choice = prompt<int>("Enter 1 to continue or 0 to quit",2);
cout << endl;
}
while (choice != 0);
cout << "\nGood-bye!\n";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。