我正在为Java 1做一个项目,我完全坚持这个问题.
基本上我需要将字母串中的每个字母加倍.
"abc" -> "aabbcc"
"uk" -> "uukk"
"t" -> "tt"
Run Code Online (Sandbox Code Playgroud)
我需要在一个被认为是"Java 1"值得的while循环中完成它.所以我猜这意味着更多的问题.
根据我的知识,我知道对我来说最简单的方法是在while循环中使用charAt方法,但由于某种原因,我的思想无法弄清楚如何将字符作为字符串返回到另一个方法.
谢谢
[编辑]我的代码(错误,但也许这会有所帮助)
int index = 0;
int length = str.length();
while (index < length) {
return str.charAt(index) + str.charAt(index);
index++;
}
Run Code Online (Sandbox Code Playgroud) 我想在一个循环中添加一个变量,但我不希望它超越(在第二次迭代y将是200)x值,我希望y到达x但是首先添加参数100,我怎么能实现这个?
int x = 130;
int y = 0;
while(y<x)
{
y += 100;
}
Run Code Online (Sandbox Code Playgroud) 基本的纸牌游戏,在while循环中有一个switch语句.case是用户输入选择的卡号,然后更换.那部分工作正常.但测试默认语句(任何数字不是0-5),它进入默认的console.writeline的无限循环,我不知道为什么它没有破坏.在以前的开关嵌套在while循环中它对我有效,但我从来没有在以前的默认值之前有这个无限循环问题.任何帮助/提示?
int userInput;
string userString;
Console.WriteLine("Would you like to replace a card?");
Console.WriteLine("Select which card you would like to replace, 1-5. Enter 0 to skip");
userString = Console.ReadLine();
int.TryParse(userString, out userInput);
while (userInput != 0)
{
switch (userInput)
{
case 1:
userInput--;
userHand[userInput] = cardDeck.GetOneCard();
break;
case 2:
userInput--;
userHand[userInput] = cardDeck.GetOneCard();
break;
case 3:
userInput--;
userHand[userInput] = cardDeck.GetOneCard();
break;
case 4:
userInput--;
userHand[userInput] = cardDeck.GetOneCard();
break;
case 5:
userInput--;
userHand[userInput] = cardDeck.GetOneCard();
break;
default:
Console.WriteLine("Incorrect input, try again");
break; …Run Code Online (Sandbox Code Playgroud) 我有这个声明.当有空格或;作为空格时应该退出char.但是当这些条件中的任何一个为真时它都不会退出循环; 当我使用&&它时效果更好但现在预计(显然)两个条件都是正确的.这仍然没有帮助我.
while ( !pt.get(locCursor).equals(';') || !pt.get(locCursor).equals(' ')){
word = word + pt.get(locCursor);
if (locCursor < pt.size()-1){
locCursor ++;
}else{
break;
}
}
Run Code Online (Sandbox Code Playgroud) package pack;
import java.util.Scanner;
public class Calculator {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
String cont = "Yes";
while(cont == "Yes" || cont == "yes" ){
System.out.print("Enter a Number: ");
int x = scan.nextInt();
System.out.print("Enter another Number: ");
int y = scan.nextInt();
int diff = x - y;
int sum = x + y;
int prod = x * y;
int quot = x / y;
System.out.println("The Sum is: " + sum);
System.out.println("The Diffrence is: " …Run Code Online (Sandbox Code Playgroud) 我有一个while循环需要每秒重复一次,我不知道该怎么做.我甚至从未使用过Java的时间模块或其他任何模块.
基本循环如下所示.任何人都可以给我一个如何做到这一点的小费?
while (!isOver()) {
// do some work
}
Run Code Online (Sandbox Code Playgroud) 我有两个数字表,说[1, 2, 3, 4, 5]和[7, 8, 9, 10, 11],我想形成由与在第二列表中的每个成员的第一个列表每个成员的产品的一个新的列表.在这种情况下,新列表中将有5*5 = 25个元素.
到目前为止,我一直无法使用while()循环.这是我到目前为止:
x = 0
y = 99
results = []
while x < 5:
x = x + 1
results.append(x*y)
while y < 11:
y = y + 1
results.append(x*y)
Run Code Online (Sandbox Code Playgroud) 答案可能是面对我,但我不确定为什么我收到这个错误.我if else在我的代码底部的语句中收到令牌"系统"上的语法错误.类似于第131行的所有行都接收到错误.任何帮助将不胜感激.
到目前为止完整的代码
import java.io.*;
import java.lang.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Assignment3 {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileReader("AssistantHoursAndRates.txt"));
double UnitRM1;
System.out.println("Enter recommended maximum staff cost of Unit 1");
UnitRM1 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit1 = "+UnitRM1);
System.out.printf("%10s\n", " ");
double UnitRM2;
System.out.println("Enter recommended maximum staff cost of Unit 2");
UnitRM2 = …Run Code Online (Sandbox Code Playgroud) 为什么以下while语句永远循环?我想在用户输入"是"或"否"后突破.其他一切都应该让他留在输入循环中.
while ($input -ne "yes" -or $input -ne "no") {
$input = Read-Host "Ready to process? [yes|no]"
switch ($input) {
yes {
write-host "yes"
}
no {
write-host "no"
}
default {
write-host "input not understood"
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个while循环,在输入位置或文件名之前抛出异常.这是代码:
cout << "enter file name or location> " << flush;
while (true)
{
string thefilename;
getline( cin, thefilename );
thefile.open( thefilename.c_str() );
if (thefile) break;
cout << "Invalid file. Please enter file name or location> " << flush;
}
while(getline(thefile, temp))
cout << temp << endl;
thefile.clear();
thefile.open("blabla.txt");
cout << endl;
thefile.close();
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我跑这个我得到
enter file name or location>Invalid file. Please enter file name or location>
Run Code Online (Sandbox Code Playgroud)
代替
enter file name or location>
Run Code Online (Sandbox Code Playgroud) while-loop ×10
java ×6
loops ×4
arrays ×1
c# ×1
c++ ×1
calculator ×1
char ×1
default ×1
if-statement ×1
list ×1
math ×1
powershell ×1
python ×1
syntax ×1
syntax-error ×1
visual-c++ ×1