1 ~ 7 범위의 난수를 생성하고 1 ~ A, 2 ~ B .... 7 ~ G를 할당합니다. 난수가 생성되면 루프가 끝날 때까지 해당 숫자를 카운터에 추가 한 다음 출력합니다. 생성 된 문자의 수 내가 입력 할 때 달성하려고하는 것은 10. 출력을 얻어야합니다. Count는 다음과 같습니다.
A : 1
B : 3
C : 1
D : 2
E : 2
F : 1
G : 0
생성 된 숫자를 세면 입력 인 10이 더해 지지만 10이라고 입력하면 다음과 같이 표시됩니다.
A : 3
B : 4
C : 5
D : 5
E : 7
F : 8
G : 11
import javax.swing.JOptionPane;
public class sheet10t3
{
public static void main(String[] args)
{
String menu = "How many random notes would you like?\n(input must be in the range 10 to 50)?", results = "";
int input = Integer.parseInt(JOptionPane.showInputDialog(null,menu));
int aNumber, countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF = 0, countG = 0;
if(input < 1 || input > 50)
results = "Please enter a valid input in the range 1 to 50.";
else
{
for(int i = 0; i <= input; i++)
{
aNumber = (int) (Math.random() * 7 + 1);
switch(aNumber)
{
case 1: countA++;
case 2: countB++;
case 3: countC++;
case 4: countD++;
case 5: countE++;
case 6: countF++;
case 7: countG++;
}
}
results = "Count of each note is as follows:";
results += "\nA: " + countA;
results += "\nB: " + countB;
results += "\nC: " + countC;
results += "\nD: " + countD;
results += "\nE: " + countE;
results += "\nF: " + countF;
results += "\nG: " + countG;
}
JOptionPane.showMessageDialog(null,results);
}
}
case 1: countA++;
break;
case 2: countB++;
break;
case 3: countC++;
break;
case 4: countD++;
break;
case 5: countE++;
break;
case 6: countF++;
break;
case 7: countG++;
break;
해결
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다