- принимать из стандартного потока ввода данные, соответствующие количеству нот в строкемелодии и количества нот для смещения вверх от среднего A.
- допускаются произвольные сдвиги относительно среднего A.
- print, в стандартный поток вывода, результирующая мелодия
- при использовании цикла while.
и подсказок.
run:
Введите количество нот в строке мелодии: 5
Введите сдвиг (0 -8) над средним A или -1 для случайного сдвига: 2
Вот ваша мелодия!
(C ,1/2) (C ,1/2) (C ,1/2) (C ,1/2) \ (A ,1/2)
/(C ,1/2) (C ,1/2) (C ,1/2) \ ( А,1/2) / (Д,1)
(С,1/2) (С,1/2) \ (А,1/2) / (Д,1) / (Е,1)
(C,1/2) \ (A,1/2) / (D,1) / (E,1) / (F,1)
(E,1/2) \ (D, 1) / (E,1) / (F,1) / (G,1)
(C,1/2) / (E,1/2) \ (D,1) / (E,1 ) / (Ф ,1)
(C ,1/2) (C ,1/2) / (E ,1/2) \ (D ,1) / (E ,1)
(C ,1/ 2) (С ,1/2) (С ,1/2) / (Е ,1/2) \ (D ,1)
(С ,1/2) (С ,1/2) (С ,1/2) (C ,1/2) / (E ,1/2) (E ,2)
run :
Введите количество нот в строке мелодии : 4
Введите сдвиг (0 -8) выше среднего A или -1 для случайного сдвига : 5
Вот ваша мелодия !
(F ,1/2) (F ,1/2) (F ,1/2) \ (D ,1/2)
/(F ,1/2) (F ,1/2) \ (D ,1/2) / (G ,1)
(F ,1/2) \ (D ,1/2) / (G ,1) / (A ,1)
(A ,1/2) \ (G ,1 ) / (А ,1) / (B ,1)
(F ,1/2) / (A ,1/2) \ (G ,1) / (A ,1)
(F ,1/2 ) (F ,1/2) / (A ,1/2) \ (G ,1)
(F ,1/2) (F ,1/2) (F ,1/2) / (A ,1/2) (A ,2)
run :
Введите количество нот в строке мелодии: 6
Введите сдвиг (0–8) выше средняя A или -1 для случайного сдвига: -1
Вот ваша мелодия!
(G ,1/2) (G ,1/2) (G ,1/2) (G ,1/2) (G ,1/2) \ (E ,1/2)
/(G ,1/2) (G ,1/2) (G ,1/2) (G ,1 /2) \ (E,1/2) / (A,1)
(G,1/2) (G,1/2) (G,1/2) \ (E,1/2) / (А ,1) / (B ,1)
(G ,1/2) (G ,1/2) \ (E ,1/2) / (A ,1) / (B ,1) / (C ,1)
(G ,1/2) \ (E ,1/2) / (A ,1) / (B ,1) / (C ,1) / (D ,1)
(Б ,1/2) \ (A ,1) / (B ,1) / (C ,1) / (D ,1) / (E ,1)
(G ,1/2) / (B , 1/2) \ (A,1) / (B,1) / (C,1) / (D,1)
(G,1/2) (G,1/2) / (B ,1/2) \ (A ,1) / (B ,1) / (C ,1)
(G ,1/2) (G ,1/2) (G ,1/2) / ( B ,1/2) \ (A ,1) / (B ,1)
(G ,1/2) (G ,1/2) (G ,1/2) (G ,1/2) / (Б ,1/2) \ (А ,1)
(G ,1/2) (G ,1/2) (G ,1/2) (G ,1/2) (G ,1/2) / (B ,1/2) (B ,2)
Более того, ваша программа должна соответствовать следующему тексту программы Java, а итерация должна выполняться только с помощью циклов while. Что я сделал, так это написал программу, а затем удалил тела некоторых методов. Ваша задача — добавить инструкции обратно. Или, по крайней мере, инструкции, которые будут выполнять эту работу и использовать циклы while. За исключением добавления инструкций
в указанные методы, весь исполняемый код должен оставаться точно таким, как в сопроводительном тексте программы. – это означает, что вы можете добавлять к себе комментарии по мере необходимости. Вы сможете использовать свою программу для создания мелодий, которые появляются в моей демонстрации, а также во многих других
.
package mmw;
import note.SNote;
import java.util.Scanner;
public class LoopyMelody {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SNote note = new SNote();
note.text();
// Start off at middle A; prep for shift.
note.lp(2);
int rowPlays = askForNumberOfNotesPerRow(sc);
int pitchShift = askForPitchShift(sc);
note.rp(pitchShift);
playMelody(rowPlays, note);
// Return the pitch to middle C for invariance.
note.lp(pitchShift);
note.rp(2);
note.untext();
}
private static int askForPitchShift(Scanner sc) {
// You have to write the body of this method.
}
private static int askForNumberOfNotesPerRow(Scanner sc) {
// You have to write the body of this method.
}
private static void playMelody(int rowPlays, SNote note) {
// You must write this method. You must use while loops.
// Make good use of the methods playSamePitch and
// playRisingPitch that are written for you below.
// Start by getting the "baseline" pitch and
// the "shift note" to play correctly, and then add in the
// rising pitch sequence.
}
private static void playSamePitch(int i, SNote note) {
int j = 0;
while (j < i) {
note.play();
j = j + 1;
}
}
private static void playRisingPitch(int i, SNote note) {
int j = 0;
while (j < i) {
note.rp();
note.play();
j = j + 1;
}
note.lp(i);
}
}
Вот код, который я написал, но не могу найти ошибку. Когда я запускаю его, кажется, что вывод начинается и заканчивается только с (C, 1) или (-, 1), и он не соответствует ожидаемой структуре мелодии из описания проблемы. Я думаю, что проблема может быть в методе playMelody, но я хотел попросить о помощи, так как я все еще новичок в этом, и программирование для меня немного сложно.
package mmw;
import note.SNote;
import java.util.Scanner;
public class LoopyMelody {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SNote note = new SNote();
note.text();
// Start off at middle A; prep for shift.
note.lp(2);
int rowPlays = askForNumberOfNotesPerRow(sc);
int pitchShift = askForPitchShift(sc);
note.rp(pitchShift);
playMelody(rowPlays, note);
// Return the pitch to middle C for invariance.
note.lp(pitchShift);
note.rp(2);
note.untext();
}
private static int askForPitchShift(Scanner sc) {
// You have to write the body of this method.
System.out.print("Enter the shift (0 - 8) above middle A, or -1 for a random shift: ");
int shift = sc.nextInt();
// Check for random shift
if (shift == -1) {
shift = (int) (Math.random() * 9); // Generate random shift between 0 and 8
}
// Validate input
while (shift < -1 || shift > 8) {
System.out.print("Invalid input. Please enter a value between 0 and 8, or -1 for random shift: ");
shift = sc.nextInt();
if (shift == -1) {
shift = (int) (Math.random() * 9);
}
}
return shift;
}
private static int askForNumberOfNotesPerRow(Scanner sc) {
// You have to write the body of this method.
System.out.print("Enter the number of notes per row melody: ");
return sc.nextInt(); // Return user input directly.
}
private static void playMelody(int rowPlays, SNote note) {
// You must write this method. You must use while loops.
// Make good use of the methods playSamePitch and
// playRisingPitch that are written for you below.
// Start by getting the "baseline" pitch and
// the "shift note" to play correctly, and then add in the
// rising pitch sequence.
int baselinePitch = 2; // Starting pitch (middle A)
int shiftNote = baselinePitch + pitchShift; // Calculate the shift note using the provided pitch shift
int lines = 6; // Total number of lines to print
// First part of the melody
int currentLine = 0;
while (currentLine < lines) {
int i = rowPlays; // Reset i to the number of notes per row for each line
// Play same pitch notes
playSamePitch(i, note);
// Play the shift note at the end of each line
System.out.print(" \\ ");
note.lp(shiftNote);
note.play();
System.out.println();
currentLine++; // Move to the next line
}
private static void playSamePitch(int i, SNote note) {
int j = 0;
while (j < i) {
note.play();
j = j + 1;
}
}
private static void playRisingPitch(int i, SNote note) {
int j = 0;
while (j < i) {
note.rp();
note.play();
j = j + 1;
}
note.lp(i);
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... put-format
Мобильная версия