我正在尝试解决这个程序的打印总和:(a/1)+(a/2)+(a/3)+(a/4)+ .......... + (a/n)其中a由用户输入,n的限制也由用户输入,这是我尝试的程序:
/**
* Program to
*
* Anirudh Gupta
* th August 2014
*/
import java.io.*;
public class Program87b
{
public static void main () throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(isr);
System.out.println("Enter the value of the numerator");
double a=Double.parseDouble(br.readLine());
System.out.println("Enter the limit for the denominator");
int limit=Integer.parseInt(br.readLine());
double n=1.0;
double sum=0.0;
while(n<=limit)
{
sum=+(a/n);
n++;
}
System.out.println(sum);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我输入a = 4且n = 5时我得到0.8这只是(4/5)的ans而不是(4/1)+(4/2)+(4/3)+(4的总和/ 4)+(4/5)应该是9.1333333333 ...
while (CMMC%a=!0 && CMMC%b=!0){
cout << CMMC;
CMMC++;
Run Code Online (Sandbox Code Playgroud)
嗨,大家好!
我是C++的新手,我正在努力学习,但我从基础开始.我在上述情况下有错误.逻辑是......当CMMC除以a和b不为0时,CMMC加1,然后再次检查CMMC.我知道这很愚蠢,但我只是从C++开始,我找不到解决方案.
我有兴趣骑自行车浏览我的页面上有一些div,这些div在ID中有数字.
这就是我到目前为止......
var count = 1;
while(count != 12) {
$('#logo-'.concat(count).concat(' > img')).click(function() {
if($('#info-'.concat(count)).hasClass('dropdown-active')) {
$('#info-'.concat(count)).slideUp();
$('#info-'.concat(count)).removeClass('dropdown-active');
} else {
$('#info-'.concat(count)).slideDown();
$('#info-'.concat(count)).addClass('dropdown-active');
}
return false;
});
count++;
}
Run Code Online (Sandbox Code Playgroud)
当计数到达if语句时,计数似乎停止工作.
所以页面上的ID是logo-1,info-1,logo-2,info-2等......
我想写一个用户输入的程序,只要输入不是0就会继续要求输入.
我是怎么做到的:我决定检查第一个输入是否为0,如果为0则程序将立即退出.如果第一个输入不是0,那么它将要求用户输入更多数字.
我的问题:它只询问我2次,然后执行程序结束语句.
我的守则
import java.util.Scanner;
public class MyArList {
private static final int num2 = 0;
public static void main (String[] args){
Scanner userInput = new Scanner(System.in);
System.out.print("Enter 1st number: ");
int num1 = userInput.nextInt();
if (num1==0){
System.out.println("program exits");
}
else
{System.out.print("Enter more numbers: ");
while(!(userInput.nextInt()==0))
System.out.print("Progam ends ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我也想过/尝试过,但那也不起作用
if( num1==0){
System.out.println("Program exits");
}
else{
do{
System.out.print("Enter more numbers: ");
int num2 = userInput2.nextInt();
}while(!(num2==0));
Run Code Online (Sandbox Code Playgroud)
感谢您宝贵的时间和意见.
嗨,我正在做一个相当简单的"卡拉OK"程序...我正在尝试用java鼠标点击开始的线程更改显示的文本.当没有循环并且我重复点击鼠标它可以工作但是当我将无限的while循环添加到thread.run()时它变得卡住......它什么也没做......我做错了什么?这是我的代码:
public class Timer extends Thread {
MainWindow window;
public int timeSec;
ArrayList<Integer> times;
public Song song;
public Timer(MainWindow window){
times = new ArrayList<Integer>();
times.add(10); // de alto
times.add(50); // el carino
times.add(70); // cuando juanita
times.add(92); // Limpia el
times.add(113); // de alto
times.add(160); // sabes
times.add(215); // la cosa esta + o.J
times.add(226); // mira
times.add(244); // ref
times.add(266); // matus
times.add(272); // Janka + krik
times.add(293); // mira
song = new Song();
this.window = window; …Run Code Online (Sandbox Code Playgroud) 我正在制作一个石头剪刀游戏.一切正常,除了我无法理解为什么第二个while循环不能在这段代码中工作.我想制作程序,以便如果用户没有输入"R"或"P"或"S",那么用户将被告知这是一个无效的条目,它将提示用户再次输入他们的答案.它对于player1完全正常,但它不适用于player2.对于player2,如果你没有输入"R"或"P或"S",那么它会提示你再次输入一个新值,但只会输入一次,无论你输入什么.所有的帮助表示赞赏!
if playGame == "Y":
print("Ok, here we go.")
player1 = input("Player1, what is your choice, R, P, or S? ")
player1 = player1.upper()
while player1 != 'R' and player1 != 'P' and player1 != 'S':
player1 = input("Invalid answer. Please answer R, P, or S: ")
player1 = player1.upper()
player2 = input("Player2, what is your choice, R, P, or S? ")
player2 = player2.upper()
while player2 != 'R' and player2 != 'P' and player2 != 'S':
player2 = input("Invalid …Run Code Online (Sandbox Code Playgroud) public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Random r = new Random();
intro();
int numGames = 0;
int numGuesses = game(console, r);
int max = max(numGuesses);
String again = "";
while (again.startsWith("y") || again.startsWith("Y")) {
game(console, r);
System.out.println("Do you want to play again?");
again = console.next();
numGames++;
}
stats(numGames, numGuesses, max);
}
Run Code Online (Sandbox Code Playgroud)
这是我的方法main中的代码,代码应该调用其他方法来编写带数字的猜谜游戏.我设置了我的while循环,以便它将运行游戏,并且在游戏运行后,它将询问用户他/她是否想再次玩游戏.然后用户输入任何内容,如果他/她键入的字符串以"y"或"Y"开头,则游戏将再次播放.(除此之外,假设用户没有输入"n"或"N"以外的任何其他字母,那么程序将转到方法调用stats();)
问题是,在游戏运行一次并且用户猜对了之后,程序甚至不再要求再次播放,它只是直接进入统计数据(); 方法调用.我究竟做错了什么?如何修复它以便它会要求用户再次播放并且只要用户键入任何以"y"或"Y"开头的单词就会继续播放?
这就是我到目前为止所拥有的.这个想法是它应该增加n直到n ^ 3小于12000,并打印出n低于12k的最高整数.
public static void main(String[] args) {
int n = 0;
int nCubed = (int) (Math.pow(n, 3));
while (nCubed < 12000) {
n++;
}
System.out.println("The highest integer below 12000 is " +n);
}
}
Run Code Online (Sandbox Code Playgroud) System.out.println("Enter number of dice to throw, an integer [2, 10]: ");
Scanner keyboard = new Scanner (System.in);
n = keyboard.nextInt();
//if the input is valid
if (n>1 && n<11)
{`
System.out.println("good");
Random rn = new Random();
int random = rn.nextInt((6-1) +1) +1;
System.out.println("random number is " + random);
}
else
{
//if the users input is invalid
while (n<2 && n>10)
{
System.out.println("error, must be in [2,10] ");
n = keyboard.nextInt();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在练习红宝石.在其中一个练习中,它要求我尝试使用'while'循环来打印数字1到50.(向上计数)我还有一个倒计时的代码.
i = 50
while i > 0 do
print i
i -= 1
end
Run Code Online (Sandbox Code Playgroud)
无论如何,你可以使上面的代码计数?
谢谢
while-loop ×10
java ×6
loops ×6
algorithm ×1
c++ ×1
html ×1
if-statement ×1
integer ×1
javascript ×1
jquery ×1
python ×1
ruby ×1
string ×1