gpt4 book ai didi

java - 尝试在我的java代码中打印数组

转载 作者:行者123 更新时间:2023-12-02 09:01:13 28 4
gpt4 key购买 nike

我有两个名为 Guitar 和 TestGuitar 的独立文件,试图执行代码来获取琴弦数量、吉他长度、品牌、颜色,然后随机选择 16 个音符和时值。我可以编译和执行我的代码,但无法弄清楚如何让它打印吉他代码的 playGuitar() 部分中的信息。任何关于如何前进的想法或意见都会非常有帮助和赞赏。这是 Guitar.java 的第一个代码

/*
* File: Guitar.java
* Author:
* Date: Feb 8, 2020
* Purpose:
*/

import java.util.Random;

public class Guitar {
private int numStrings;
private double guitarLength;
private String guitarManufacturer;
private String guitarColor;

//Constructor
public Guitar(int numStrings, double guitarLength, String guitarManufacturer, String guitarColor) {
this.numStrings = numStrings;
this.guitarLength = guitarLength;
this.guitarManufacturer = guitarManufacturer;
this.guitarColor = guitarColor;
}

// Default Constructor
public Guitar() {
this.numStrings = 6;
this.guitarLength = 28.2;
this.guitarManufacturer = "Gibson";
this.guitarColor = "Red";
}

// Setter methods
// setnumStrings
public void setnumStrings(int numStrings) {
this.numStrings = numStrings;
}
// setguitarLength
public void setguitarLength(double guitarLength) {
this.guitarLength = guitarLength;
}
// setguitarManufacturer
public void setguitarManufacturer(String guitarManufacturer) {
this.guitarManufacturer = guitarManufacturer;
}
// setguitarColor
public void setguitarColor(String guitarColor) {
this.guitarColor = guitarColor;
}

// Getter methods
// getnumStrings
public int getnumStrings() {
return numStrings;
}
// getguitarLength
public double getguitarLength() {
return guitarLength;
}
// getguitarManufacturer
public String getguitarManufacturer() {
return guitarManufacturer;
}
// getguitarColor
public String getguitarColor() {
return guitarColor;
}

// playGuitar method for 16 randomly selected musical notes
public String playGuitar() {
String play = "[";
//arrays containing possible notes and duration
char[] notes = {'A','B','C','D','E','F','G'};
double[] duration = {0.25,0.5,1,2,4};
//integers works as index for the arrays above
int a;
int b;
//initiate a random number
Random rn = new Random();
//for loop to select 16 randomly selected musical notes of random duration
for (int k=0;k<16;k++) {
a = rn.nextInt(7);
b = rn.nextInt(5);
play = play + notes[a] + "(" + String.valueOf(duration[b]) + ")";
if (k!=15) {
play = play + ",";
}
}
play = play + ']';
System.out.println(play);
return play;
}

// toString method
public String toString() {
return "toString(): (numStrings = " + numStrings + ", guitarLength = " +
guitarLength + ", guitarManufacturer = " + guitarManufacturer +
", guitarColor = " + guitarColor + ")";
}
}

这是TestGuitar代码的第二部分

/*
* File: TestGuitar.java
* Author:
* Date: Feb 8, 2020
* Purpose:
*/
import java.util.Random;

public class TestGuitar extends Guitar{
public TestGuitar(int numStrings, double guitarLength, String guitarManufacturer, String guitarColor) {
super(numStrings, guitarLength, guitarManufacturer, guitarColor);
}
public static void main(String[] args) {
Guitar one = new Guitar(7, 30.2, "Fender", "Black");
System.out.println(one.toString());
System.out.println("getNumStrings(): " + one.getnumStrings());
System.out.println("getGuitarLength(): " + one.getguitarLength());
System.out.println("getGuitarManufacturer(): " + one.getguitarManufacturer());
System.out.println("getGuitarColor(): " + one.getguitarColor());
}


}

最佳答案

也许我不明白你的问题,但是,你为什么不尝试一下

System.out.println(one.playGuitar());

关于java - 尝试在我的java代码中打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60140306/

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