- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在创建一个程序,其中一个数字将由 2 位用户进行因式分解,并且在不使用原始数字的情况下将其分解为 1 的人获胜。我应该使用循环,但我不知道哪个是最好的,也不知道如何构造它,才不会像我的大多数程序那样令人费解。
在文本中,它的结构应该是这样的。
输入一个数字 N 作为要因式分解的数字。 2 玩家将轮流输入一个数字来分解 N 倍。因数不能小于2,必须均分N。
程序应该循环这个循环,每次向每个玩家 1 询问一个新号码,直到号码为 1。我现在将发布我的代码,我不确定如何继续,或者我是否正在使用右循环,因为 while 通常用于以条件为假为前提的语句。
#include <stdio.h>
int main(){
int gameN;
int factor;
printf("What number should the game be played with?\n");
scanf("%d", &gameN);
while (gameN != 1) {
{
printf("The current number is %d .Current Player enter a factor.\n", gameN);
scanf("%d", &factor);
if (gameN%factor==0){
printf("the current number is %d . Current Player enter a factor.\n");
scanf("%d", &factor);
if (gameN/factor== 1){
break; printf("Game Over. You won!");
}
if (factor<2) {
printf(" Invalid Factor. Numbers less than 2 are not allowed. The current number is %d. Current Player, enter a factor.\n", gameN);
scanf("%d", &factor);
return 0;
感谢任何人的帮助。
最佳答案
在 scanf("%d", &factor);
之后可能会发生三件坏事
gameN
在每种情况下,您都希望继续
循环。 C 中的 continue
语句导致循环从顶部重新开始。
作为旁注,您可以检查 scanf
的返回值以确保 scanf
有效。它将返回成功转换的次数,在您的代码中应该为 1。
以下代码显示了如何使用continue
语句重新启动循环,直到用户输入有效数字。它还展示了如何刷新输入,以防用户输入垃圾,如“fdajkl”。
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int gameN, factor, count;
printf( "What number should the game be played with?\n" );
if ( scanf( "%d", &gameN ) != 1 )
exit(1); // user didn't enter a valid number, so just quit
while ( gameN != 1 )
{
// get a factor from the user
printf("The current number is %d. Current Player enter a factor.\n", gameN);
count = scanf( "%d", &factor );
// check for all the bad things that can happen
if (count != 1 || gameN % factor != 0 || factor < 2 )
{
scanf( "%*[^\n]" ); // read and discard any characters up to the newline
getchar(); // read and discard the newline character
printf( "That's not a valid input\n" ); // notify the user
continue; // go back to the top of the loop to try again
}
gameN /= factor; // divide the game value by the factor
if ( gameN == 1 ) // check for victory
{
printf( "Game Over. You won!\n" );
break;
}
}
return 0;
}
关于c - 因子博弈,使用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491273/
我有这种格式的data.frame: 'data.frame': 244 obs. of 1 variable: $ names: Factor w/ 244 levels "ERA","BA
这就是问题: write a Java Program that accepts a String and an integer stretch factor P as parameters and
该示例显示了不同工厂的产量测量值,第一列表示工厂最后一列是生产量。 factory % mutate(factory=fct_lump(factory,2)) factory produc
我正在使用分类变量运行回归并遇到 this question .在这里,用户想要为每个虚拟对象添加一列。这让我很困惑,因为我虽然列有很长的数据,包括使用 as.factor() 存储的所有虚拟数据。相
假设在 R 中有一个 Data.Frame 对象,其中所有字符列都已转换为因子。然后我需要“修改”与数据帧中某一行相关联的值——但将其编码为一个因子。我首先需要提取一行,所以这就是我正在做的。这是一个
利用下面的可重现数据, dat head(dat) Bin Number 1 1 3 2 1 5 3 1 4 4 1 5 5 1
我有一组包含多个变量的数据。其中一个变量 - 阶乘包含组的名称 - A、B、C 等。其余变量是数字。 > data1 Group Value 1 A 23 2 A
我有一组编码为二项式的变量。 Pre VALUE_1 VALUE_2 VALUE_3 VALUE_4 VALUE_5 VALUE_6 VALUE_7 VALUE_8 1 1 0
我的问题与 this one 非常相似和 this other one ,但我的数据集有点不同,我似乎无法使这些解决方案起作用。如果我误解了什么并且这个问题是多余的,请原谅。 我有一个这样的数据集:
我一直在尝试生成一个带有离散 x 变量的堆积面积图(因为我想显示财政年度,即“2013/14”,而不是日历年)。但是,将 x 轴变量转换为一个因子会阻止在最终图表中呈现 geom。 有解决办法吗? l
只是一个简单的问题来确认我的想法, 使用负载因子 1.0 的哈希表的复杂性将是二次时间,用以下符号 O(n^2) 表示。 这是因为必须不断调整大小并一遍又一遍地插入。如果我错了,请纠正我。 谢谢 最佳
我正在尝试使用 kaggle 的一些数据集进行房价预测。 这是我的代码 library(ggplot2) dataset=read.csv('train(1).csv') dataset_test=r
我正在用 Angular 构建一个类似咆哮的 UI。我想将其公开为工厂(或服务),以使其在我的 Controller 中可用。调用 Growl.add 将导致 DOM 发生变化,所以看起来我应该有一个
我正在尝试将 pandas 数据框的一列转换为因数,因为我试图在 R 中调用的函数需要因数。 pandas2ri.activate() #second column of labels has
我正在尝试使用 plotly 绘制一个以字符串(组合数)作为 x 轴的条形图。 (“1”、“2”、“3”、“4 - 5”、“6 - 8”、“9 - 13”、“14 - 21”、“22 - 34”、“3
我有一个包含 NA 的数据集。 此外,它还有一些列需要factors()。 我正在使用 caret 包中的 rfe() 函数来选择变量。 似乎 rfe() 中的 functions= 参数使用 lmF
我有一个 .csv 文件,其中每个字段用于日期时间、日期和时间。 最初它们都是字符字段,我已经相应地转换了它们。 在我的代码结束时,如果我这样做: str(data) 我会得到 datetime: P
我有一个如下所示的数据集: data.flu data.flu chills runnyNose headache fever flu 1 1 0 M
我正在使用 QMainWindow 在 C++ 中手动布置 Qt 应用程序。我希望在屏幕底部有两个并排停靠的小部件,但我希望它们具有不成比例的宽度。目前,我只能让它们具有相同的宽度。有没有办法设置拉伸
我需要通过在两个主机(2 个 Java 进程)之间发送合成调用来计算 VOIP 质量。我应该找出 MOS、抖动和 R 因子(VOIP 质量指标)。根据目前的研究,我发现我应该在两台主机之间发送 RTP
我是一名优秀的程序员,十分优秀!