В Замене буквы 4.3.6 CodeHS требует, чтобы пользователь ввел слово (word), буква, которая будет заменена (letterToReplace) и замену этой буквы (newLetter). Затем программа должна заменить все НО ПЕРВЫЙ экземпляр LetterToReplace на newLetter. Я продолжаю получать две ошибки, как бы я ни пытался это исправить. Я получаю, что int не может быть преобразовано в String, а String не может быть преобразовано в int. Буду признателен за любую помощь, поскольку я все еще изучаю Java.
Ниже приведен мой код:
import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Ask the user for 3 things: their word, letter they want to replace,
// and replacing letter.
System.out.println("Enter a word:");
String word = input.nextLine();
System.out.println("Enter the letter to be replaced:");
String lTP = input.nextLine();
System.out.println("Enter the new letter:");
String nL = input.nextLine();
// Call the method replaceLetter and pass all 3 of these items to it for
// string processing.
String editedWord = replaceLetter(word, lTP, nL);
System.out.println(editedWord);
}
// Modify this method so that it will take a third parameter from a user --
// the String with which they want to replace letterToReplace
//
// This method should replace all BUT the first occurence of letterToReplace
// You may find .indexOf to be useful, though there are several ways to solve this problem.
// This method should return the modified String.
public static int replaceLetter(String word, String letterToReplace, String newLetter)
{
int count = 0;
String newWord = "";
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
newWord += newLetter;
}
else
{
word.substring(i, i+1);
}
}
return word;
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... 3-6-codehs
Заменить букву 4.3.6 CodeHS ⇐ JAVA
Программисты JAVA общаются здесь
1728921034
Anonymous
В Замене буквы 4.3.6 CodeHS требует, чтобы пользователь ввел слово (word), буква, которая будет заменена (letterToReplace) и замену этой буквы (newLetter). Затем программа должна заменить все [b]НО ПЕРВЫЙ[/b] экземпляр LetterToReplace на newLetter. Я продолжаю получать две ошибки, как бы я ни пытался это исправить. Я получаю, что int не может быть преобразовано в String, а String не может быть преобразовано в int. Буду признателен за любую помощь, поскольку я все еще изучаю Java.
Ниже приведен мой код:
import java.util.Scanner;
public class Letter
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Ask the user for 3 things: their word, letter they want to replace,
// and replacing letter.
System.out.println("Enter a word:");
String word = input.nextLine();
System.out.println("Enter the letter to be replaced:");
String lTP = input.nextLine();
System.out.println("Enter the new letter:");
String nL = input.nextLine();
// Call the method replaceLetter and pass all 3 of these items to it for
// string processing.
String editedWord = replaceLetter(word, lTP, nL);
System.out.println(editedWord);
}
// Modify this method so that it will take a third parameter from a user --
// the String with which they want to replace letterToReplace
//
// This method should replace all BUT the first occurence of letterToReplace
// You may find .indexOf to be useful, though there are several ways to solve this problem.
// This method should return the modified String.
public static int replaceLetter(String word, String letterToReplace, String newLetter)
{
int count = 0;
String newWord = "";
for(int i = 0; i < word.length(); i++)
{
if(word.substring(i, i+1).equals(letterToReplace))
{
newWord += newLetter;
}
else
{
word.substring(i, i+1);
}
}
return word;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79086851/replace-letter-4-3-6-codehs[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия