是否有更好的方法通过消除ifC语句中的重复条件来编写以下代码?
while (n < 0) {
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: please enter a positive integer\n");
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
在这个程序中,我试图返回一个新字符串,该字符串由添加的新字母和旧字母组成(如果不符合约束条件).我不知道如何修复我的代码,以便正确打印.非常感谢任何帮助或建议!
这里有些例子:
str:"asdfdsdfjsdf",单词:"sdf",c:"q"
应该返回"aqdqjq",我得到"asdqqq"
str:"aaaaaaaa",字:"aaa",c:"w"
应该返回"wwaa",截至目前我的代码只返回"ww"
public static String replaceWordWithLetter(String str, String word, String c)
String result = "";
int index = 0;
while (index < str.length() )
{
String x = str.substring(index, index + word.length() );
if (x.equals(word))
{
x = c;
index = index + word.length();
}
result = result + x;
index++;
}
if (str.length() > index)
{
result = result + str.substring(index, str.length() - index);
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个程序,查看用户输入的字母是否在字符串“ hello”中,如果是,请打印该字符串在字符串中以及字符串在字符串中的位置。错误是“二进制运算符的错误操作数类型”
String str = "hello", guess;
int testing = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a letter: ");
guess = scan.nextLine(); // Enters a letter
// finds the letter in the string
while (str.charAt(testing) != guess && testing != 6) {
testing++; // Continues loop
}
//prints where letter is if it is in the string
if (str.charAt(testing) == guess)
System.out.println("The letter is at "+testing);
else
System.out.println("Could not find that letter.");
Run Code Online (Sandbox Code Playgroud) 我一直试图解决这个问题大约5天..找不到任何解决方案请发送帮助.我应该实现一个函数来按值"删除"数组中的每个元素.假设我的数组是"Hello",我想删除每个"l".到目前为止,我只能删除一次.顺便说一下,请记住我不允许使用指针来实现这个功能......(我们学校还没有学到这一点)这是我的代码:
#include <stdio.h>
#include <string.h>
void strdel(char array[], char c);
int main(void)
{
char source[40];
printf("\nStrdel test: ");
strcpy(source, "Hello");
printf("\nsource = %s", source);
strdel(source, 'l');
printf("\nStrdel: new source = %s", source);
return 0;
}
void strdel(char array[], char c)
{
int string_lenght;
int i;
for (string_lenght = 0; array[string_lenght] != '\0'; string_lenght++) {}
for (i = 0; i < string_lenght; i++) {
if (array[i] == c) {
for (i = i; array[i] != '\0'; ++i)
array[i] = array[i + …Run Code Online (Sandbox Code Playgroud) 问题:您将获得不同面额的金币和总金额.编写一个函数来计算构成该数量所需的最少数量的硬币.如果这笔钱不能由任何硬币组合弥补,则返回-1.
例1:
输入:coins = [1,2,5],金额= 11输出:3说明:11 = 5 + 5 + 1
例2:
输入:coins = 2,金额= 3输出:-1
You may assume that you have an infinite number of each kind of coin.
我的代码:
public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
int new_amount=amount;
int count_coin=0;
int q=0,r=0,a=0;
int k=coins.length-1;
while(amount>0 && k>=0) {
q = new_amount / coins[k];
count_coin = count_coin + q;
r = new_amount % coins[k];
new_amount=r;
a+=q*coins[k];
k--;
}
if(a==amount){
return count_coin;
}
else{
return -1;
} …Run Code Online (Sandbox Code Playgroud) 快速简单的问题:
这是:
private static void SetupConnection()
{
try
{
TcpClient client = new TcpClient(myServer, myPort);
//Do whatever...
}
catch (SocketException)
{
//Server is closed. Retry in 10 minutes.
Thread.Sleep(600000);
SetupConnection();
}
Run Code Online (Sandbox Code Playgroud)
一个可行的替代方案:
private static void SetupConnection()
{
while (true)
{
try
{
TcpClient client = new TcpClient(myServer, myPort);
//Do whatever...
break;
}
catch (SocketException)
{
//Server is closed. Retry in 10 minutes.
Thread.Sleep(600000);
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然第二个看起来"更干净",但如果第一个也是可以接受的话,我仍然很好奇 - 如果不是,那么为什么不呢?
我有2个内部while循环。代码将结果写到矩阵的第一行,但是当结果到达第二行时,它仅传递其他行,而不填充行的列。怎么解决呢?代码和输出如下所示:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
Run Code Online (Sandbox Code Playgroud)
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a …Run Code Online (Sandbox Code Playgroud) 我试图用输入编写贪婪算法,该算法应返回更改中要使用的最小硬币数,但不返回任何值。我不知道为什么。它只是要求输入,然后什么也不显示。
我创建了一个先前的线程,其中确定了一个导致无限循环的错误,该错误被压缩,但是现在我的逻辑中似乎还有另一个潜在的错误。
#include <stdio.h>
#include <cs50.h>
#include <math.h>
// declare variable change_owed, num_coins, and input globally
float change_owed = 0;
float input;
int num_coins;
int main(void)
{
// makes sure the input is non-negative
do
{
input = get_float("Amount paid\n");
}
while(input <=0);
// begin checking
while(input > 0)
{
if(input - .25 >= 0) // quarters
{
num_coins++; // number of coins used, to be printed later, is incremented
input = input - .25; // coin is subtracted from …Run Code Online (Sandbox Code Playgroud) 当随机选择列表中的特定元素时,我需要在选择函数中使用while循环来终止
我已经成功调用choice()函数来选择一个随机元素,但是当我尝试添加一个while循环时却遇到了无限循环
from random import *
while choice != 3:
choice([1,2,3,4,5,6])
print(choice)
print('damn!')
Run Code Online (Sandbox Code Playgroud)
我得到一个无限循环。我需要打印所有随机生成的数字,这些数字不是while循环终止的条件。当达到终止条件时,将显示一个字符串,程序将终止
在这段代码中,迭代应该比它们早于一个位置停止。
k = 0
while 2^k < 5:
k += 1
print(k)
Run Code Online (Sandbox Code Playgroud)
我希望它打印出“ 3”,但它打印出“ 4”。