小编Tia*_*Tia的帖子

Integer.MAX_VALUE和Integer.MIN_VALUE的解释,用于查找数组中的最小值和最大值

我似乎不知道如何Integer.MAX_VALUEInteger.MIN_VALUE找到在一个数组的最小值和最大值的帮助.

我理解这个方法(下面的伪代码)在找到最小值和最大值时是如何工作的:

max = A[0], min = A[0]
for each i in A
  if A[i] > max then max = A[i]
  if A[i] < min then min = A[i] 
Run Code Online (Sandbox Code Playgroud)

但至于这种方法,我不明白的目的Integer.MAX_VALUEInteger.MIN_VALUE:

import java.util.Scanner;

class MyClass {

    public static void main(String[] args) {

        int[] numbers; // declaring the data type of numbers
        numbers = new int[3]; //assigning the number of values numbers will contain
        int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE;

        Scanner input = …
Run Code Online (Sandbox Code Playgroud)

java arrays numbers

26
推荐指数
3
解决办法
16万
查看次数

Java-增加一个类变量的计数器

我有一个问题,其中一部分说:

Vehicle 类有 4 个属性,即 noOfTyres、accessories、brand 和 counter,其类型分别为整数、布尔、字符串和整数。Counter 是一个类变量。该类的构造函数初始化所有 3 个变量并将计数器加一。

我为这部分想到了两种方法,但我不确定哪一种是正确的,或者两者是否都是正确的。

第一个是:

public class Vehicle{
  private int noOfTyres;
  private Boolean accesories;
  private String brand;
  private int static counter=0;
  private int counterNum;

public Vehicle(int noOfTyres, int accessories, int brand){
 counter++;
 this.noOfTyres= noOfTyres;
 this.accessories= accessories;
 this.brand= brand;
 counterNum= counter;}

}
Run Code Online (Sandbox Code Playgroud)

第二个是:

  public class Vehicle{
   private int noOfTyres;
   private Boolean accesories;
   private String brand;
   private int counter=0;


public Vehicle(int noOfTyres, int accessories, int brand){
 counter++;
 this.counter= counter;
 this.noOfTyres= noOfTyres;
 this.accessories= accessories;
 this.brand= …
Run Code Online (Sandbox Code Playgroud)

java constructor

4
推荐指数
1
解决办法
1万
查看次数

MQTT:将消息发布到代理时,Android应用程序崩溃

我正在尝试将有关rpi / gpio的消息发布到我的MQTT代理(这是我的Raspberry Pi)。发送该消息的目的是打开/关闭连接到我的Raspberry Pi的LED(类似于教程,但该教程适用于Swift而非Android)。因此,当我在Android应用程序上打开switch小部件时,消息“ on”将发布到Raspberry Pi代理以将其打开(通过也连接到Raspberry Pi上的MQTT的python脚本)。没有所有的MQTT代码,下面的片段通过显示开关可以很好地工作。但是,使用以下代码,它将崩溃。

public class ControlsFragment extends Fragment {

    private ControlsFragment.OnFragmentInteractionListener listener;

    public static ControlsFragment newInstance() {
        return new ControlsFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_controls, container, false);

        Switch switch1= (Switch)view.findViewById(R.id.tempSwitch);
        switch1.setChecked(true);
        switch1.setTextOn("On");
        switch1.setTextOff("Off");

        String clientId = MqttClient.generateClientId();
        MqttAndroidClient client =
                new MqttAndroidClient(this.getActivity(), "tcp://192.168.100.8:1883",
                        clientId);

        try {
            IMqttToken token = client.connect();
            token.setActionCallback(new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken …
Run Code Online (Sandbox Code Playgroud)

java android mqtt

4
推荐指数
1
解决办法
728
查看次数

Java:抽象类方法和接口

下面的程序计算实例化的任意数量形状的区域总数.问题是我不知道如何输出接口的draw()方法.

这是主要类:

public class MyClass1{

public static void main(String[] args) {


    Shape[] shapes= new Shape[3];


    shapes[0]= new Circle(20.0);
    shapes[1]= new Rectangle(25, 63);
    shapes[2]= new Circle(25);

   double total=0;

  for(int i=0; i<shapes.length; i++){

    total+= shapes[i].area();

}

System.out.printf("Total: %.1f", total);

  }

 }
Run Code Online (Sandbox Code Playgroud)

超类形状

 abstract class Shape {

  abstract double area();

}
Run Code Online (Sandbox Code Playgroud)

Drawable接口

public interface Drawable {
    public abstract void draw();

}
Run Code Online (Sandbox Code Playgroud)

子类圈

public class Circle extends Shape implements Drawable {

    double radius;

    Circle(double aRadius){

    radius= aRadius;
}


   double area(){

       return Math.PI*radius*radius; …
Run Code Online (Sandbox Code Playgroud)

java interface abstract

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

java:在构造函数中递增id以在main中显示

我正在编写一个程序来模拟咖啡店中的系统,其中接受客户订单的人给每个客户一个代币号,并将客户的代币号和他/她订购的物品一起输入系统.该recordOrder功能执行该操作,该操作允许输入订单细节,订单由票证ID,tID和表示订单中的项目的字符串的数组列表示.

这是我到目前为止所做的:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

public class Ques4 {

private static class Order{
    private static int tID=0;
    private int orderId;
    private String itemName;

    public Order(String itemName){
        tID++;
        this.orderId=tID;
        this.itemName=itemName;

    }

    public int getOrderId(){
        return orderId;
    }

    public void setOrderId(int orderId){
        this.orderId=orderId;
    }

    public int gettID() {
        return tID;
    }

    public void settID(int tID) {
        this.tID = tID;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName …
Run Code Online (Sandbox Code Playgroud)

java arraylist data-structures

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

Java:具有返回类型的继承方法

Class3课程延伸至Class1课程.在Class3,该方法应该返回奖金的总额和工资,并且Class1继承Class3并显示总数并通过将总数乘以12来计算年薪.问题在于我得到的总和的输出yearlySalary是0.0.

以下是代码:

class MyClass extends MyClass3{
    double yearlySalary = 12.0*total;

    public static void main(String[] args) {
        MyClass obj1 = new MyClass();

        System.out.println("Employee's salary is: "+ obj1.salary);
        System.out.println("Employee's bonus is: "+ obj1.bonus);
        System.out.println("Total: "+ obj1.total);
        System.out.println("Yearly Salary: "+ obj1.yearlySalary);
    }
}
Run Code Online (Sandbox Code Playgroud)

二等:

public class MyClass3 { 
    double salary =40000;
    double bonus = 2000;
    double total;       

    public double CalcTotal(){
        total = salary+bonus;
        return total;
    }
}
Run Code Online (Sandbox Code Playgroud)

java inheritance return

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