Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建一种方法,通过用户输入的数量增加长方形的高度和宽度_Java - Fatal编程技术网

Java 创建一种方法,通过用户输入的数量增加长方形的高度和宽度

Java 创建一种方法,通过用户输入的数量增加长方形的高度和宽度,java,Java,好的,在课堂上,我一直在做一些基于长方形课堂的长方形问题。我遇到的问题是创建一种方法,将长方形的高度和宽度增加用户定义的量 这是我的主要观点: import java.util.Scanner; public class Oblong6 { public static void main(String [] args) { Oblong ob1 = new Oblong(); Scanner keyboardIn = new Scanner(Sy

好的,在课堂上,我一直在做一些基于长方形课堂的长方形问题。我遇到的问题是创建一种方法,将长方形的高度和宽度增加用户定义的量

这是我的主要观点:

    import java.util.Scanner;

public class Oblong6
{
   public static void main(String [] args)
   {

      Oblong ob1 = new Oblong();

      Scanner keyboardIn = new Scanner(System.in);

      Oblong ob = new Oblong();

      double h, w, x;
      System.out.print ("Enter the height of the oblong:");
      h = keyboardIn.nextDouble();

      System.out.print ("Enter the width of the oblong:");
      w = keyboardIn.nextDouble();

      System.out.print (" Enter the amount to increment by ");
      x = keyboardIn.nextInt();

      ob.setHeight(h);
      ob.setWidth(w);
      ob.setX(x);

      System.out.println (ob.incHeight());
      System.out.println (ob.incWidth());

      System.out.println("Height " + h);
      System.out.println("Width " + w);


   }
}
这是我在oblong类中创建的增加它们的方法:

public class Oblong
{
    // instance variables
    private double height;
    private double width;
   private double x;

    // constructor
    public Oblong()
   {

        height = 0.0;
        width = 0.0;
      x = 0.0;
    }

    // methods
    public double getHeight()
    {   
        return height;      
    }

    public double getWidth()
    {   
        return width;       
    }
   public double setX(double x)
   {
      return x;
   }
    public void setWidth(double w)
    {   
        width = w;
    }

    public void setHeight(double h)
    {   
        height = h;
    }

    public double calculateArea()
    {   
        return width * height;
    }   

   public double calculatePerimeter()
   {
      return width + height * 2;

   }

   public boolean isSquare()
   {
      if(height == width)
      {
         return true;
      }
      else
      {
         return false;
      }
    }
   public double incHeight()
    {
      { 
        return height + x ; 

       }
   }
   public double incWidth()
   {
      {
         return width + x ;
      }
   }
}// end of class
但它只打印出原始高度和宽度。

其中:

 public double incWidth()
   {
      {
         return width + x ;
      }
   }
您将返回宽度+1,但不修改私有属性。只要这样做:

public double incWidth(){
    this.width = this.width + 1;
    return this.width;
}
同样在二传中,你不需要返回任何东西。要更改类内的属性,请执行以下操作:

private double value;

private void setValue( double value ) {
   this.value = value;
}
使用this.value可以引用类中的private值。如果没有,您将引用方法的参数值设置值

进一步阅读:

关于这一点:

 public double incWidth()
   {
      {
         return width + x ;
      }
   }
您将返回宽度+1,但不修改私有属性。只要这样做:

public double incWidth(){
    this.width = this.width + 1;
    return this.width;
}
同样在二传中,你不需要返回任何东西。要更改类内的属性,请执行以下操作:

private double value;

private void setValue( double value ) {
   this.value = value;
}
使用this.value可以引用类中的private值。如果没有,您将引用方法的参数值设置值

进一步阅读:
您的实例变量
x
尚未设置,因此
height+x
将返回
height+0.0

更改此项:

public double setX(double x)
   {
      return x;
   }
为此:

public double setX(double x)
   {
      this.x = x;
   }
这将返回要显示的值,但应设置为供以后使用,因此您需要:

public void incHeight()
    {
        setHeight(height + x) ; 
    }
然后:

System.out.println("Height " + height); // height  not h

您的实例变量
x
尚未设置,因此
height+x
将返回
height+0.0

更改此项:

public double setX(double x)
   {
      return x;
   }
为此:

public double setX(double x)
   {
      this.x = x;
   }
这将返回要显示的值,但应设置为供以后使用,因此您需要:

public void incHeight()
    {
        setHeight(height + x) ; 
    }
然后:

System.out.println("Height " + height); // height  not h
您的代码:

public double incHeight()
{
  { 
    return height + x ; 

   }
}
只需将两个数字相加并返回结果。 它不做任何事情。其他方法也是如此

然而,该方法的目的似乎是改变底层对象的状态。但正如所说;当前实现不会改变该状态

希望这足以帮助您自己解决问题

旁注:阅读Java语法。你额外的一副牙套。。。也不做任何事情。

您的代码:

public double incHeight()
{
  { 
    return height + x ; 

   }
}
只需将两个数字相加并返回结果。 它不做任何事情。其他方法也是如此

然而,该方法的目的似乎是改变底层对象的状态。但正如所说;当前实现不会改变该状态

希望这足以帮助您自己解决问题



旁注:阅读Java语法。你额外的一副牙套。。。也不做任何事情。

我们能看到
Oblong
的完整代码吗?另外,请张贴您使用的输入、实际输出和预期输出。我已经为oblong添加了完整的代码。我使用的输入是高度10宽度10增加5。这将返回10,10或者我将交换它们,使用5,5增加10,结果将是5,5。旁注:如果您将其称为“矩形”而不是“长方形”。。。“wtf是一个长方形”会为我节省几秒钟吗?它不起作用,因为“x”没有设置,请检查
setX(double x)
,它应该是
this.x=x
@Jägermeister是的,我知道这正是我们笔记中所说的。在其他一些问题上玩得很开心,试图找到长方形周长等的公式。。。没有人这么叫它。我们能看到
Oblong
的完整代码吗?另外,请张贴您使用的输入、实际输出和预期输出。我已经为oblong添加了完整的代码。我使用的输入是高度10宽度10增加5。这将返回10,10或者我将交换它们,使用5,5增加10,结果将是5,5。旁注:如果您将其称为“矩形”而不是“长方形”。。。“wtf是一个长方形”会为我节省几秒钟吗?它不起作用,因为“x”没有设置,请检查
setX(double x)
,它应该是
this.x=x
@Jägermeister是的,我知道这正是我们笔记中所说的。在其他一些问题上玩得很开心,试图找到长方形周长等的公式。。。没人这么叫。谢谢。是的,我现在可以用了。是的,我想我添加了额外的,因为上面的方法是一个if/else,它有额外的{},我没有注意到。请注意:我非常喜欢总是使用
{blocks}
。。。甚至当我们讨论if/then/else或只使用一条语句的循环时,使用块到底是什么意思?我的意思是
if(foo){bar();}
if(foo)bar()好{blocks}
。。。甚至当我们讨论if/then/else或只使用一条语句的循环时,使用块到底是什么意思?我的意思是
if(foo){bar();}
if(foo)bar()好谢谢那个链接非常有用。谢谢那个链接非常有用。