小编Mar*_*oun的帖子

按钮没有出现,空指针异常

我正在学习使用java.swing库.我正在尝试创建一个非常简单的计算器布局.我添加了addNumbers方法.我试图在计算器中显示按钮,我已经用于loops.buttons没有出现我正在获得nullpointerexception.

import java.awt.*;
import javax.swing.*;

public class Calculator extends JFrame{

    /**
     * @param args
     */
    //dEFINE WIDTH AND HEIGHT
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;

    //Values for buttons having numbers
    private JButton[] numButton;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Calculator myCalculator = new Calculator();

    }

    public Calculator(){
        setTitle("Simple Calculator");
        setSize(WIDTH,HEIGHT);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Container Pane = getContentPane();
        Pane.setLayout(new GridLayout(3,3));

        //Add numbers to screen now
        addNumbers(Pane);



    }

    //Function to …
Run Code Online (Sandbox Code Playgroud)

java arrays swing

0
推荐指数
1
解决办法
59
查看次数

为什么这段代码出现"无法访问的语句"错误?

我有以下方法:

  char getChar(int I)
  {
    if (I<65+26) return (char)(I);

    switch (I)
    {
      case 91 : return '?';break;
      case 92 : return '#';break;
      default : return ' ';
    }
  }
Run Code Online (Sandbox Code Playgroud)

为什么会出现"无法访问的语句"错误?

java compiler-errors unreachable-code

0
推荐指数
1
解决办法
73
查看次数

为什么我们需要递归?

使用递归然后循环(for,while,do-while)有什么好处?

使用递归(这里我得到一个给定数字的总和,假设数字是5然后5 + 4 + 3 + 2 + 1:

   #include<stdio.h>
   int sum(int n);
   void main()
    {
     int sums =0, n;
     scanf("%d",&n);
     sums = sum(n);
     printf("%d",sums);

       while (1)
        {

        }
    }

  int sum(int n)
  {
     if (n==1)
     return 1;
     return n + sum(n-1);
  }
Run Code Online (Sandbox Code Playgroud)

没有递归(这里我得到一个给定数字的总和假设数字是5然后5 + 4 + 3 + 2 + 1:

  #include<stdio.h>

  void main()
   {
     int sum =0, n;
     scanf("%d",&n);
     for(int i=n;i>=1;i--)
      {
       sum = sum + i;
      }
      printf("%d",sum);

      while (1)
      {
      }
    }
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
941
查看次数

*a = b和a =&b之间的差异?

两者之间有什么不同,如果有的话

*a = b;
Run Code Online (Sandbox Code Playgroud)

a = &b;
Run Code Online (Sandbox Code Playgroud)

这两种不同的方法是将var指向另一个var吗?

c pointers

0
推荐指数
1
解决办法
112
查看次数

如何区分数字和字符串

我有一个字符串

String s="Raymond scored 2 centuries at an average of 34 in 3 innings.";
Run Code Online (Sandbox Code Playgroud)

我需要找到字符串中只有数字的总和,而不会遇到任何异常.这里的总和应该是2 + 34 + 3 = 39.如何让编译器理解String和Integer之间的区别.

java string integer

0
推荐指数
1
解决办法
3706
查看次数

为什么`Integer [100] arr;`无效,而`Ineger [] arr;`有效?

我试图为100个项目分配一个Integer数组,为什么这个声明在Java中无效?

Integer[100] intArr1;  ----- (1)
Run Code Online (Sandbox Code Playgroud)

这是有效的:

Integer[] intArr;      ----- (2)
Run Code Online (Sandbox Code Playgroud)

由于(2)有效,它占用了多少内存?任何人都可以帮忙解释一下.

在SO中有一些问题类似于我的问题,但是它们并不相同,我在问这个问题之前确实看过了.

java arrays

0
推荐指数
1
解决办法
72
查看次数

SSH 在运行 bash 之前没有颜色

当我 ssh 进入另一台机器时,所有文本都变成白色。但是,当我运行 cmd“bash”时,我得到彩色输出。

echo $0输出“-bash”。

运行命令“bash”并更改为彩色输出后,输出 echo $0“bash”,不带“-”。

当我 ssh 到其他机器时,如何才能将彩色 bash 作为标准,而不必使用命令“bash”并退出 x2 来断开连接。

bash terminal

0
推荐指数
1
解决办法
3204
查看次数

如何在括号“)(”上拆分字符串?

输入:

val str="(2500 - Analytical Charge Percentage of Monitoring Structure (MS) type Sub-Business Unit (SSBU) : 803.130000000000000000)(388 - Monitoring Structure (MS) type Sub-Business Unit (SSBU) : JzCddaxT)"
Run Code Online (Sandbox Code Playgroud)

想要在 () 中使用两个更大的字符串作为数组的元素。在这种情况下,如果我们在 ")(" 上拆分:

预期输出:

arr(0) = "(2500 - Analytical Charge Percentage of Monitoring Structure (MS) type Sub-Business Unit (SSBU) : 803.130000000000000000"

arr(1) = "388 - Monitoring Structure (MS) type Sub-Business Unit (SSBU) : JzCddaxT)"
Run Code Online (Sandbox Code Playgroud)

我正在使用 str.split("[\\\\)\\\\(]")但它不起作用。

java regex string scala

0
推荐指数
1
解决办法
69
查看次数

哪个错了?Java,数学还是我......?困惑...!

我有以下SOP:

System.out.println(9 - ((9 / 2) * 2));
Run Code Online (Sandbox Code Playgroud)

从我在学校学到的数学,它应该评估0.

但我得到的输出是1...... !!!

我已经尝试在输出中评估Google中的相同表达式0.

根据数学,Java评估为1,它被评估为0.任何解释?

java expression arithmetic-expressions

-1
推荐指数
1
解决办法
135
查看次数

增加指针的值不起作用

该程序应该使用指针计算相似条目的数量,但每当我输入数字时,计数器总是等于零.我出了什么问题?

#include <stdio.h>
#include <stdlib.h>

//using function to count similar enteries in an array...
void count_similar_enteries(int array_func[10],int *number, int *ptr_to_counter);

int main()
{
    int number = 0;
    int array[10] = {0,1,1,2,3,1,2,67,65,1};
    int counter = 0;
    printf("enter a number\n");
    scanf("%d", &number);
    count_similar_enteries(array, &number,&counter);
    printf("the number of similar enteries are %d\n", counter);
    return 0;
}

void count_similar_enteries(int array_func[10],int *number, int *ptr_to_counter)
{
    int i;
    for (i = 0; i< 10 ; i++)
    {
        if(array_func[i] == *number)
        {
            *ptr_to_counter++;
            continue;
        }
        else
        { …
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
1
解决办法
149
查看次数