gpt4 book ai didi

java - 类未返回预期的输出

转载 作者:行者123 更新时间:2023-11-30 01:41:16 25 4
gpt4 key购买 nike

我正在编写一个Guitar37类,其中将37个键映射到从110Hz到880Hz的色标。

public static final String KEYBOARD =
"q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "; // keyboard layout


给定此字符串,字符串的第i个字符对应于440 * 2(i-24)/ 12的频率。例如:字符'q'为110Hz,'i'为220Hz,'v'为440Hz, “”(空格键)为880Hz。

吉他37课

public class Guitar37 implements Guitar {
public static final String KEYBOARD =
"q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "; // keyboard layout

private GuitarString[] strings;
private int currentTime;

public Guitar37() {
currentTime = 0;
strings = new GuitarString[37];

for (int i = 0; i < 37; i++) {
strings[i] = new GuitarString(440 * Math.pow(2, (double) (i - 24) / 12));
}
}

public void playNote(int pitch) {
int indexPitch = pitch + 24;

if (indexPitch < 37 && indexPitch > 0) {
strings[indexPitch].pluck();
}
}

public boolean hasString(char key) {
return KEYBOARD.indexOf(key) != -1;
}

public void pluck(char key) {
if(!hasString(key)) {
throw new IllegalArgumentException();
}

strings[KEYBOARD.indexOf(key)].pluck();
}

public double sample() {
double sum = 0.0;

for (int i = 0; i < 37; i++) {
sum += strings[i].sample();
}
return sum;
}

public void tic() {
for (int i = 0; i < 37; i++) {
strings[i].tic();
}
currentTime++;
}

public int time() {
return currentTime;
}
}


运行Test37类以测试Guitar37类是否返回正确的输出时,输出中第一个音符的声音样本应为:

Playing note -24 (initially 110.25)
time 0 sample = 110.25
time 1 sample = 100.25
time 2 sample = 90.25
time 3 sample = 80.25
Plucking string 'q'
time 4 sample = 110.25
time 5 sample = 100.25
time 6 sample = 90.25
time 7 sample = 80.25
making an extra call on tic


相反,我的Guitar37类返回:

Playing note -24 (initially 110.25)
time 0 sample = 0.0
time 1 sample = 0.0
time 2 sample = 0.0
time 3 sample = 0.0
Plucking string 'q'
time 4 sample = 110.25
time 5 sample = 100.25
time 6 sample = 90.25
time 7 sample = 80.25
making an extra call on tic


所有其他注释产生正确的输出,但该类仅在0到4的时间内返回0.0,而不是4到7的值。

Test37类

// This is a program that can be used to test the basic functionality of the
// Guitar37 class. It does not test all of the functionality.
//
// The main method has an array of 37 frequency values that should be produced
// by a correctly working Guitar37 class. The actual frequencies don't all end
// in 0.25, but this version of GuitarString converts them to that form so that
// it is easier to understand the output produced by the program. In
// particular, it is easier to see if it is properly adding new values as notes
// are played.

import java.util.*;

public class Test37 {
public static final String KEYBOARD =
"q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "; // keyboard layout

public static void main(String[] args) {
double[] frequencies =
{110.25, 116.25, 123.25, 130.25, 138.25, 146.25, 155.25, 164.25,
174.25, 184.25, 195.25, 207.25, 220.25, 233.25, 246.25, 261.25,
277.25, 293.25, 311.25, 329.25, 349.25, 369.25, 391.25, 415.25,
440.25, 466.25, 493.25, 523.25, 554.25, 587.25, 622.25, 659.25,
698.25, 739.25, 783.25, 830.25, 880.25};

Guitar g = new Guitar37();
compare(frequencies, GuitarString.nums);
for (int i = 0; i < KEYBOARD.length(); i++) {
int note = i - 24;
System.out.println("Playing note " + note + " (initially " +
frequencies[i] + ")");
g.playNote(note);
advance(g, 4);
char key = KEYBOARD.charAt(i);
System.out.println("Plucking string '" + key + "'");
if (g.hasString(key)) {
g.pluck(key);
advance(g, 4);
} else {
System.out.println("ERROR: not recognizing key '" + key + "'");
}
System.out.println("making an extra call on tic");
// throw in an extra call on tic without calling time and sample
g.tic();
System.out.println();
}

// now test a few unsupported values of pitch which should be ignored
// but should not throw an exception
int[] unsupportedPitch = {-32, -25, 13, 18};
for (int n : unsupportedPitch) {
System.out.println("testing playNote for unsupported pitch " + n);
g.playNote(n);
advance(g, 4);
System.out.println();
}
}

// This method advances the simulation the given number of tics reporting
// the time reading and sample values from the given guitar.
public static void advance(Guitar g, int tics) {
for (int i = 0; i < tics; i++) {
System.out.println("time " + g.time() + " sample = " + g.sample());
g.tic();
}
}

// This method compares the array nums with the set of numbers in nums2,
// suspending program execution if the two sets of numbers differ.
public static void compare(double[] nums, Set<Integer> nums1) {
Set<Integer> nums2 = new TreeSet<>();
for (double n : nums) {
nums2.add((int) n);
}
if (!nums1.equals(nums2)) {
System.out.println("Wrong frequencies for Guitar37 construction.");
System.out.println("should be approximately: " + nums2);
System.out.println("yours are approximately: " + nums1);
System.exit(1);
}
}
}


GuitarString类

// This class is used for debugging the Guitar37 class.  It is not an example
// to be emulated. When a string is plucked, it is set to the integer part of
// the string's frequency plus 0.25. It goes down by 10 each time tic is
// called until it becomes less than or equal to 10 when it is set to 0.

import java.util.*;

public class GuitarString {
static Set<Integer> nums = new TreeSet<>(); // observed frequency values
double value;
double freq;

public GuitarString(double frequency) {
freq = frequency;
nums.add((int) frequency);
}

public void pluck() {
value = (int) freq + 0.25;
}

public void tic() {
if (value <= 10) {
value = 0.0;
} else {
value = value - 10;
}
}

public double sample() {
return value;
}
}

最佳答案

我在您的代码中看到的错误在这里:

strings[i] = new GuitarString(440 * Math.pow(2, (double) (i - 24) / 12));


您正在做 (i - 24) / 12(这是int除法),然后强制转换为double。

代替:

strings[i] = new GuitarString(440 * Math.pow(2, (i - 24) / 12.0));


这将执行双重除法而不是整数除法。

关于java - 类未返回预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59888383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com