- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 java 还很陌生,我们并没有真正讨论这些方法,但它是项目所必需的。我试图将整个两个 do-while
循环放入一个可以在 public void run()
中调用的方法中。有人可以指导我如何实现它吗?我试图将这个带有内部和外部 do-while
循环的嵌套循环放在它自己的方法中,也可能放在它自己的类中
完整代码如下:do
循环开始的部分我试图将其放入自己的方法中,我该如何完成呢?
App.java
import acm.program.*;
public class App extends ConsoleProgram
{
public void init()
{
setSize(300,600);
}
public void run()
{
// Counter variables
int n = 0;
int counter = 0;
int score = 0;
n = readInt("How many times did you take this quiz: ");
/*
* Printing questions
*/
// First Question
Questions questionOne = new Questions();
println(" ");
questionOne.SetQuestion("Question 1: ");
questionOne.SetChoice1("Answer a: ");
questionOne.SetChoice2("Answer b: ");
questionOne.SetChoice3("Answer c: ");
questionOne.SetChoice4("Answer d: ");
questionOne.SetCorrectAnswer("b");
// Second Question
Questions questionTwo = new Questions();
println(" ");
questionTwo.SetQuestion("Question 2: ");
questionTwo.SetChoice1("Answer a: ");
questionTwo.SetChoice2("Answer b: ");
questionTwo.SetChoice3("Answer c: ");
questionTwo.SetChoice4("Answer d: ");
questionTwo.SetCorrectAnswer("d");
// Third Question (Hard Question)
Questions questionThree = new Questions();
println(" ");
questionThree.SetQuestion("Question 3: ");
questionThree.SetChoice1("Answer a: ");
questionThree.SetChoice2("Answer b: ");
questionThree.SetChoice3("Answer c: ");
questionThree.SetChoice4("Answer d: ");
questionThree.SetCorrectAnswer("a");
// CREATE A METHOD FOR THIS DO-WHILE LOOP.
do
{
if ( n <= 3 )
{
println("You may now take the quiz!");
println(" ");
}
else
{
println("You're out of quiz attempts. You have already taken it 3 times.");
break;
}
do
{
score = 0;
if (counter==5)
{
println("You scored less than 100%");
println("Take a break! You may retake the quiz on a later date. ");
break;
}
// print question 1
println(questionOne.PrintQuestion());
// input answer for question number 1
String yourAnswer = readLine("Your answer: ");
println(" ");
// check if the question 1 is correct, if it is, add 1 to the score++
if (questionOne.IsAnswerCorrect(yourAnswer))
{
score++;
println("You got it right!");
}
else // if its not correct, print you got it wrong
{
println("You got it wrong!");
println(" ");
println(" ");
}
// print question 2
println(questionTwo.PrintQuestion());
// input answer for question number 2
String yourAnswer2 = readLine("Your answer: ");
println(" ");
// check if the question 2 is correct, if it is, add 1 to the score++
if (questionTwo.IsAnswerCorrect(yourAnswer2))
{
score++;
println("You got it right!");
}
else // if its not correct, print you got it wrong
{
println("You got it wrong!");
println(" ");
println(" ");
}
// print question 3
println(questionThree.PrintQuestion());
// input answer for question number 3
String yourAnswer3 = readLine("Your answer: ");
println(" ");
// checking for hard question. To make it easier, we're using boolean to check for the hard question
boolean hardQuestion = false;
// check if the question 3 is correct, if it is, add 1 to the score++ // hard question becomes true
if (questionThree.IsAnswerCorrect(yourAnswer3))
{
score++;
println("You got it right!");
println(" ");
hardQuestion = true;
}
else
{
println("You got it wrong!");
println(" ");
println(" ");
}
if (score == 0)
{
println("You need more practice!");
println(" ");
}
// if you got at least the hard one right, your score is of 1 and that is what gets printed out
else if (score == 1)
{
if (hardQuestion)
{
println("Well, at least you got the hard one correct! ");
println(" ");
}
else
{
// if the other ones are correct, print out this message if hard is wrong
println("You need to warm up!");
println(" ");
}
}
// if you got at least two correct
else if (score == 2)
{
if (hardQuestion)
{
// user got one correct and hard one correct
println("You are amazing!");
println(" ");
}
else
{
// if hard one was wrong
println("You're on your way!");
println(" ");
}
}
else
{
// all answers are correct including the hard ones
println("You are a scholar!");
println(" ");
println("Congradulations you passed! You got 100% on the quiz!");
break;
}
// count number of tries the quiz has been looped
counter++;
if (counter < 5)
{
println("You scored less than 100%");
println(" ");
println(" ");
println("Try again!");
println(" ");
}
} while (true); // end program loop
break;
} while (true); // end while loop
} // closing public run
} // closing public class
私有(private)字符串问题代码:
Questions.java
public class Questions {
private String _question;
private String _correctAnswer;
private String _correctAnswer2;
private String _correctAnswer3;
private String _choice1, _choice2, _choice3, _choice4;
public void SetQuestion(String q)
{
_question = q;
}
public String GetQuestion()
{
return _question;
}
public void SetCorrectAnswer(String c)
{
_correctAnswer = c;
}
public String GetCorrectAnswer()
{
return _correctAnswer;
}
public void SetCorrectAnswer2(String b)
{
_correctAnswer2 = b;
}
public String GetCorrectAnswer2()
{
return _correctAnswer2;
}
public void SetCorrectAnswer3(String a)
{
_correctAnswer3 = a;
}
public String GetCorrectAnswer3()
{
return _correctAnswer3;
}
public void SetChoice1(String a)
{
_choice1 = a;
}
public void SetChoice2(String b)
{
_choice2 = b;
}
public void SetChoice3(String c)
{
_choice3 = c;
}
public void SetChoice4(String d)
{
_choice4 = d;
}
public String PrintQuestion()
{
String result = " ";
result = _question + "\n" + _choice1 + "\n" + _choice2 + "\n" + _choice3 + "\n" + _choice4 + "\n";
return result;
}
public boolean IsAnswerCorrect(String a)
{
return _correctAnswer.equals(a);
}
/*public boolean IsAnswerCorrect2(String b)
{
return _correctAnswer2.equals(b);
}*/
}
最佳答案
您可以使用此代码或根据需要进行必要的修改,只是它有一个方法,该方法采用问题
数组,然后通过从数组中取出问题来执行操作
import acm.program.*;
public class App extends ConsoleProgram
{
int n = 0;
int counter = 0;
int score = 0;
public void init()
{
setSize(300,600);
}
public void run()
{
// reset counter variables
n = 0;
counter = 0;
score = 0;
n = readInt("How many times did you take this quiz: ");
/*
* Printing questions
*/
// First Question
Questions questionOne = new Questions();
println(" ");
questionOne.SetQuestion("Question 1: ");
questionOne.SetChoice1("Answer a: ");
questionOne.SetChoice2("Answer b: ");
questionOne.SetChoice3("Answer c: ");
questionOne.SetChoice4("Answer d: ");
questionOne.SetCorrectAnswer("b");
// Second Question
Questions questionTwo = new Questions();
println(" ");
questionTwo.SetQuestion("Question 2: ");
questionTwo.SetChoice1("Answer a: ");
questionTwo.SetChoice2("Answer b: ");
questionTwo.SetChoice3("Answer c: ");
questionTwo.SetChoice4("Answer d: ");
questionTwo.SetCorrectAnswer("d");
// Third Question (Hard Question)
Questions questionThree = new Questions();
println(" ");
questionThree.SetQuestion("Question 3: ");
questionThree.SetChoice1("Answer a: ");
questionThree.SetChoice2("Answer b: ");
questionThree.SetChoice3("Answer c: ");
questionThree.SetChoice4("Answer d: ");
questionThree.SetCorrectAnswer("a");
evaluate(new Questions[]{questionOne,questionTwo,questionThree});
} // closing public run
private void evaluate(Questions[] q){
// METHOD FOR DO-WHILE LOOP.
do
{
if ( n <= 3 )
{
println("You may now take the quiz!");
println(" ");
}
else
{
println("You're out of quiz attempts. You have already taken it 3 times.");
break;
}
do
{
score = 0;
if (counter==5)
{
println("You scored less than 100%");
println("Take a break! You may retake the quiz on a later date. ");
break;
}
// print question 1
println(q[0].PrintQuestion());
// input answer for question number 1
String yourAnswer = readLine("Your answer: ");
println(" ");
// check if the question 1 is correct, if it is, add 1 to the score++
if (q[0].IsAnswerCorrect(yourAnswer))
{
score++;
println("You got it right!");
}
else // if its not correct, print you got it wrong
{
println("You got it wrong!");
println(" ");
println(" ");
}
// print question 2
println(q[1].PrintQuestion());
// input answer for question number 2
String yourAnswer2 = readLine("Your answer: ");
println(" ");
// check if the question 2 is correct, if it is, add 1 to the score++
if (q[1].IsAnswerCorrect(yourAnswer2))
{
score++;
println("You got it right!");
}
else // if its not correct, print you got it wrong
{
println("You got it wrong!");
println(" ");
println(" ");
}
// print question 3
println(q[2].PrintQuestion());
// input answer for question number 3
String yourAnswer3 = readLine("Your answer: ");
println(" ");
// checking for hard question. To make it easier, we're using boolean to check for the hard question
boolean hardQuestion = false;
// check if the question 3 is correct, if it is, add 1 to the score++ // hard question becomes true
if (q[2].IsAnswerCorrect(yourAnswer3))
{
score++;
println("You got it right!");
println(" ");
hardQuestion = true;
}
else
{
println("You got it wrong!");
println(" ");
println(" ");
}
if (score == 0)
{
println("You need more practice!");
println(" ");
}
// if you got at least the hard one right, your score is of 1 and that is what gets printed out
else if (score == 1)
{
if (hardQuestion)
{
println("Well, at least you got the hard one correct! ");
println(" ");
}
else
{
// if the other ones are correct, print out this message if hard is wrong
println("You need to warm up!");
println(" ");
}
}
// if you got at least two correct
else if (score == 2)
{
if (hardQuestion)
{
// user got one correct and hard one correct
println("You are amazing!");
println(" ");
}
else
{
// if hard one was wrong
println("You're on your way!");
println(" ");
}
}
else
{
// all answers are correct including the hard ones
println("You are a scholar!");
println(" ");
println("Congradulations you passed! You got 100% on the quiz!");
break;
}
// count number of tries the quiz has been looped
counter++;
if (counter < 5)
{
println("You scored less than 100%");
println(" ");
println(" ");
println("Try again!");
println(" ");
}
} while (true); // end program loop
break;
} while (true); // end while loop
}
} // closing public class
关于Java do-while 在其自己的类的方法内循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33472241/
我在这里打破了头,请帮忙。 我正在抓取一个网站。 .MyElement 容器包含我尝试获取的 gif 或 jpg 源 url。 我在我的 node.js 应用程序中使用基于 Cheerio 的 .ea
尝试使用 dictionaryWithObjectsAndKeys 向字典添加值,我想使用 for 循环添加对象,但我找不到正确的出路,请帮忙,使用 for 循环仅显示最后一个值,其他值不出现.看起来
我在 for 循环中有一个 lambda,lambda 中有循环变量参数。当我运行它时,我希望输出数字 0-9。但由于它是一个 lambda,x 不会立即得到评估。 for( int x =
我正在尝试在 C# 中的循环内插入一条数据库记录。 当我像这样对值进行硬编码时它会起作用: string query3 = "INSERT INTO furniture (room_id,me
仍在处理我在 Haskell 中的 SHA1 实现。我现在有了一个可行的实现,这是内部循环: iterateBlock' :: Int -> [Word32] -> Word32 -> Word32
我试图在具有相同类名的 div 内循环。这是为了获得正确的查询字符串,我想稍后将其用于 getJSON 。问题是我无法从 div 中获取各种参数... 这是创建 div 的代码(这个工作正常,我用 C
我想要实现的是 Qt Widget 循环。 简单的例子: UI_dialog 是一个 QDialog,接受后它将打开 UI_mainwindow,它是一个 QMainWindow。 UI_mainwi
谁能解释一下这个算法的时间复杂度是多少? for (i = 1; i = n + n/2 + n/3 ... n/n但是是 < n + 1 + n/2 + 1 + n/3 + 1 + n/4 + 1.
这个问题已经有答案了: JavaScript setTimeOut doesn't seem to work like I expect (1 个回答) 已关闭 5 年前。 为了最佳实践,JS 的相对
我在 Multimap 中创建了 Integer 和 Multimap,并且尝试循环获取键和值。然而,对于 Multimap,它以数组形式返回,我无法删除该数组并仅获取 int。 这是我的代码 pub
我想遍历我所有的 delicious.com 书签并在它们周围添加一个链接... 这是我的测试站点: http://dev.thomasveit.com/json.html $(document).r
我正在尝试执行一些逻辑,它将编辑 minheap.heapList 的长度。我对 Promises 还很陌生,所以我不知道如何解决这个问题。在 while 循环 中,我还将调用一些 async 函数。
我是 VBA 新手,刚开始学习循环。 这是我的代码: sub worksheet_change(byval target as range) application.screenupdating =
我在 R 中运行以下脚本。 如果我使用 %do% 而不是 %dopar% 脚本工作正常。但是,如果在外循环中我使用 %dopar% 循环将永远运行而不会引发任何错误(内存使用量不断增加,直到内存不足)
我有一个 case 语句,如 1) 2) 3) ... 如下,其中 1) 格式 case 有多个 if else 条件。我想在无效条件下重复特定情况。当用户首先选择 1 时,它会询问果汁的名称。接下来
我正在尝试将 html block 传递给电子邮件服务 Nodemailer JS 函数(我使用的是 Node.JS)。我有一个items需要在电子邮件中发送的数组: items = [ { n
我在 page.evaluate 方法中有一个循环。该循环迭代一个查询选择器,该选择器从页面中文本元素的多个实例捕获innerText。 我收到错误评估失败:无法读取“innerText”的属性 我尝
我无法理解如何为下面的资源创建循环。我需要基于嵌套在“维度”块中的变量“实例”创建多个资源。根据我阅读一些文档后的理解,我应该使用 for_each 参数,但是我似乎无法弄清楚。 resource "
我是一名优秀的程序员,十分优秀!