gpt4 book ai didi

java - 我的类(class)没有返回完整的值范围

转载 作者:行者123 更新时间:2023-12-02 09:05:11 24 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。

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, (i - 24) / 12));
}
}

public void playNote(int pitch) {
strings[pitch + 24].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;
}
}


这是我的Guitar37类的当前版本。当我尝试对其进行测试时,预期的输出应返回所有37个频率:


  [110、116、123、130、138、146、155、164、174、184、195、207、220、233、246、261、277、293、311、329、349、369、391、415、440 ,466、493、523、554、587、622、659、698、739、783、830、880]


相反,我的班级仅返回四个频率


  [110、220、440、880]


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;
}
}


test37类(用于测试Guitar37类的类):

// 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);
}
}
}

最佳答案

尝试在您的Guitar37类中更改此设置:

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


对此:

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

关于java - 我的类(class)没有返回完整的值范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874530/

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