我只是在一本Java夏季课的书中搞一些练习因为我有点领先,这意味着这不是功课.我收到一条错误消息,指出你无法转换String为int但是两者都是字符串,一个是变量,一个是数组.
就是这条线,我遇到了麻烦...... select = items[select];
public class CarpentryChoice {
public static void main(String[] args) {
String items [] = {"Table", "Desk", "Chair", "Couch", "Lazyboy"};
int price [] = {250, 175, 125, 345, 850};
String select;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an item to view it's price: ");
select = scan.nextLine();
for(int i=0; i < items.length; i++) {
select = items[select];
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为一个夏季java课程做一个简单的任务,只是希望你们可以看看我的代码,看看我做的方式是否是最好的方法.目的是创建一个int包含至少25个元素的简单数组,并使用循环遍历它并添加所有元素.我遇到了一些问题,但看起来我已经开始工作了.在我解决之后,我做了一些研究,看到了一些类似的东西,人们使用For Each循环(增强循环).这会是一个更好的选择吗?我对使用反对常规for循环的最佳方法感到困惑.
无论如何,任何评论或批评,帮助我成为一个更好的程序员!
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
for (int i = 0; i < absencesArr.length; i++) {
absencesArr[i] += absenceTotal;
absenceTotal = absencesArr[i];
} …Run Code Online (Sandbox Code Playgroud) 我是Prolog的新手,非常困惑!我不断收到“ [WMAPDY]的单个变量”警告。我在某处读到,有时警告没有用。我还读到该程序会由于警告而不会编译所有子句?
我正在尝试执行的程序是一种“算术”难题,应该“解决” AM + PM = DAY。
如果有人可以帮助解决此错误,而且单例变量警告始终很重要,我将不胜感激!
史考特
solve([A,M,P,D,Y]):-
select(A,[0,1,2,3,4,5,6,7,8,9],WA), % W means Without
not(A=0),
select(M,WA,WMA),
select(P,WMA,WMAP),
not(P=0),
select(D,WMAP,WMAPD),
not(D=0),
select(Y,WMAPD,WMAPDY),
DAY is 100*D+10*A+Y,
AM is 10*A+M,
PM is 10*P+M,
DAY is AM+PM.
Run Code Online (Sandbox Code Playgroud) 我想知道是否还有像StringBuilder的toUpperCase这样的东西?下面是我的代码,我正在尝试用户输入一个短语并将其转换为首字母缩略词.有人通过建议StringBuilder帮助我,但我无法弄清楚是否有办法使首字母缩写大写.任何帮助是极大的赞赏.
public class ThreeLetterAcronym {
public static void main(String[] args) {
String threeWords;
int count = 0;
int MAX = 3;
char c;
//create stringbuilder
StringBuilder acronym = new StringBuilder();
Scanner scan = new Scanner(System.in);
//get user input for phrase
System.out.println("Enter your three words: ");
threeWords = scan.nextLine();
//create an array to split phrase into seperate words.
String[] threeWordsArray = threeWords.split(" ");
//loop through user input and grab first char of each word.
for(String word : threeWordsArray) {
if(count < …Run Code Online (Sandbox Code Playgroud) 我正在为班级创建一个程序,其中您有一个 JComboBox,当选择一个选项时,它会弹出一个包含不同选项的窗口。我有一个选项可以弹出一个新窗口,上面有两个按钮。
\n\n首先,我不确定是否应该使用 ItemListener 或 ActionListener 作为 JComboBox 选项。现在我有一个 ItemListener,我认为它仅适用于“矩阵”选项,但它适用于这两个选项,我不明白为什么。我将发布所有代码以防万一,但我将在指定问题的上方和下方添加星星。
\n\n感谢您的帮助或为我指明正确的方向!
\n\npublic class MultiForm extends JFrame{\n\n\n private JComboBox menu;\n private JButton bluePill;\n private JButton redPill;\n private JLabel matrix;\n private int matrixSelection;\n private static String[] fileName = {"", "The Matrix", "Another Option"};\n\npublic MultiForm() {\n super("Multi Form Program"); \n setLayout(new FlowLayout());\n menu = new JComboBox(fileName);\n add(menu);\n\n ************************************************************************* \n TheHandler handler = new TheHandler();\n menu.addItemListener(handler); \n}\n\nprivate class TheHandler implements ItemListener{\n public void itemStateChanged(ItemEvent event) {\n\n if(event.getStateChange() == ItemEvent.SELECTED) {\n menu.setSelectedItem("The …Run Code Online (Sandbox Code Playgroud) 我有一个新问题,我希望得到一些指导.
我正在创建一个GUI程序,它接收来自用户的联系信息,每次点击addButton时,都会创建一个Contact对象并将其添加到数组列表中,然后在单击viewButton时显示所有信息.
我终于把JTextField信息放到了ArrayList中,但是我不知道如何添加多个联系信息.我添加了firstNameInput.setText(""); 在输入第一个联系信息以清除屏幕但同时清除ArrayList之后.我想我需要某种循环但是我创建的循环最终会创建一个无限循环.这一切都在ActionPerformed方法中... 我理解为什么它是一个无限循环,但这是我第一次使用ArrayList,所以我感到困惑.
非常感谢指向正确方向的指针.
public class InputForm extends JFrame {
private JPanel panel;
private JLabel firstNameLabel;
private JLabel lastNameLabel;
private JLabel phoneNumLabel;
private JLabel emailLabel;
private JTextField firstNameInput;
private JTextField lastNameInput;
private JTextField phoneInput;
private JTextField emailInput;
private JButton addButton;
private JButton viewButton;
private String fn;
private String ln;
private String ph;
private String em;
private List<Contact> list = new ArrayList<Contact>();
public InputForm() {
int windowWidth = 650;
int windowHeight = 550;
//Window title
setTitle("CONTACT FORM");
//Set …Run Code Online (Sandbox Code Playgroud)